Urbit Developers
  • Lightning Tutorials

    • Introduction
    • Build a Groups App
    • Build a Chat App
    • Build a Voting App
    • Core Curriculum

      • Hoon School

        • Introduction
        • 1. Hoon Syntax
        • 2. Azimuth (Urbit ID)
        • 3. Gates (Functions)
        • 4. Molds (Types)
        • 5. Cores
        • 6. Trees and Addressing
        • 7. Libraries
        • 8. Testing Code
        • 9. Text Processing I
        • 10. Cores and Doors
        • 11. Data Structures
        • 12. Type Checking
        • 13. Conditional Logic
        • 14. Subject-Oriented Programming
        • 15. Text Processing II
        • 16. Functional Programming
        • 17. Text Processing III
        • 18. Generic and Variant Cores
        • 19. Mathematics
        • App School I

          • Introduction
          • 1. Arvo
          • 2. The Agent Core
          • 3. Imports and Aliases
          • 4. Lifecycle
          • 5. Cards
          • 6. Pokes
          • 7. Structures and Marks
          • 8. Subscriptions
          • 9. Vanes
          • 10. Scries
          • 11. Failure
          • 12. Next Steps
          • Appendix: Types
          • App School II (Full-Stack)

            • Introduction
            • 1. Types
            • 2. Agent
            • 3. JSON
            • 4. Marks
            • 5. Eyre
            • 6. React app setup
            • 7. React app logic
            • 8. Desk and glob
            • 9. Summary
          • Environment Setup
          • Additional Guides

            • Hoon Workbook

              • Competitive Programming
              • Gleichniszahlenreihe
              • Rhonda Numbers
              • Roman Numerals
              • Solitaire Cipher
              • App Workbook

                • Building a CLI App
                • Debugging Wrapper
                • Host a Website
                • Serving a JS Game
                • Ship Monitoring
                • Styled Text
                • Threads

                  • Fundamentals
                  • Bind
                  • Input
                  • Output
                  • Summary
                • Aqua Tests
                • Remote Scry
                • Command-Line Apps
                • HTTP API
                • Eyre noun channels
                • JSON
                • Generators
                • Parsing Text
                • Sail (HTML)
                • Udon (Markdown-esque)
                • Software Distribution
                • Strings
                • Unit Tests
                • Vases
                Urbit Developers
                • Lightning Tutorials

                  • Introduction
                  • Build a Groups App
                  • Build a Chat App
                  • Build a Voting App
                  • Core Curriculum

                    • Hoon School

                      • Introduction
                      • 1. Hoon Syntax
                      • 2. Azimuth (Urbit ID)
                      • 3. Gates (Functions)
                      • 4. Molds (Types)
                      • 5. Cores
                      • 6. Trees and Addressing
                      • 7. Libraries
                      • 8. Testing Code
                      • 9. Text Processing I
                      • 10. Cores and Doors
                      • 11. Data Structures
                      • 12. Type Checking
                      • 13. Conditional Logic
                      • 14. Subject-Oriented Programming
                      • 15. Text Processing II
                      • 16. Functional Programming
                      • 17. Text Processing III
                      • 18. Generic and Variant Cores
                      • 19. Mathematics
                      • App School I

                        • Introduction
                        • 1. Arvo
                        • 2. The Agent Core
                        • 3. Imports and Aliases
                        • 4. Lifecycle
                        • 5. Cards
                        • 6. Pokes
                        • 7. Structures and Marks
                        • 8. Subscriptions
                        • 9. Vanes
                        • 10. Scries
                        • 11. Failure
                        • 12. Next Steps
                        • Appendix: Types
                        • App School II (Full-Stack)

                          • Introduction
                          • 1. Types
                          • 2. Agent
                          • 3. JSON
                          • 4. Marks
                          • 5. Eyre
                          • 6. React app setup
                          • 7. React app logic
                          • 8. Desk and glob
                          • 9. Summary
                        • Environment Setup
                        • Additional Guides

                          • Hoon Workbook

                            • Competitive Programming
                            • Gleichniszahlenreihe
                            • Rhonda Numbers
                            • Roman Numerals
                            • Solitaire Cipher
                            • App Workbook

                              • Building a CLI App
                              • Debugging Wrapper
                              • Host a Website
                              • Serving a JS Game
                              • Ship Monitoring
                              • Styled Text
                              • Threads

                                • Fundamentals
                                • Bind
                                • Input
                                • Output
                                • Summary
                              • Aqua Tests
                              • Remote Scry
                              • Command-Line Apps
                              • HTTP API
                              • Eyre noun channels
                              • JSON
                              • Generators
                              • Parsing Text
                              • Sail (HTML)
                              • Udon (Markdown-esque)
                              • Software Distribution
                              • Strings
                              • Unit Tests
                              • Vases
                              Guides/Additional Guides/Threads

                              Bind

                              Having looked at form and pure, we'll now look at the last strand arm bind. Bind is typically used in combination with micgal (;<).

                              Micgal

                              Micgal takes four arguments like spec hoon hoon hoon. Given ;< a b c d, it composes them like ((b ,a) c |=(a d)). So, for example, these two expressions are equivalent:

                              ;< ~ bind:m (sleep:strandio ~s2)
                              (pure:m !>(~))

                              and

                              ((bind:m ,~) (sleep:strandio ~s2) |=(~ (pure:m !>(~))))

                              Micgal exists simply for readability. The above isn't too bad, but consider this:

                              ;< a b c
                              ;< d e f
                              ;< g h i
                              j

                              ...as opposed to this monstrosity: ((b ,a) c |=(a ((e ,d) f |=(d ((h ,g) i |=(g j))))))

                              bind

                              Bind by itself must be specialised like (bind:m ,<type>) and it takes two arguments:

                              • The first argument is a function that returns the form of a strand which produces <type>.
                              • The second argument is a gate whose sample is <type> and which returns a form.

                              Since you'll invariably use it in conjunction with micgal, the <type> in ;< <type> bind:m ... will both specialise bind and specify the gate's sample.

                              Bind calls the first function then, if it succeeded, calls the second gate with the result of the first as its sample. If the first function failed, it will instead just return an error message and not bother calling the next gate. So it's essentially "strand A then strand B".

                              Since the second gate may itself contain another ;< <type> bind:m ..., you can see how this allows you to glue together an arbitrarily large pipeline, where subsequent gates depend on the previous ones.

                              strandio

                              /lib/strandio/hoon contains a large collection of useful, ready-made functions for use in threads. For example:

                              • sleep waits for the specified time.
                              • get-time gets the current time.
                              • poke pokes an agent.
                              • watch subscribes to an agent.
                              • fetch-json produces the JSON at a particular URL.
                              • retry tries a strand repeatedly with exponential backoff until it succeeds.
                              • start-thread starts another thread.
                              • send-raw-card sends any card.

                              ...and many more.

                              Putting it together

                              Here's a simple thread with a couple of strandio functions:

                              /- spider
                              /+ strandio
                              =, strand=strand:spider
                              ^- thread:spider
                              |= arg=vase
                              =/ m (strand ,vase)
                              ^- form:m
                              ;< t=@da bind:m get-time:strandio
                              ;< s=ship bind:m get-our:strandio
                              (pure:m !>([s t]))

                              Save it as /ted/mythread.hoon of %base, |commit it and run it with -mythread. You should see something like:

                              > -mythread
                              [~zod ~2021.3.8..14.52.15..bdfe]

                              Analysis

                              To use strandio functions we've imported the library with /+ strandio.

                              get-time and get-our get the current time & ship from the bowl in strand-input. We'll discuss strand-input in more detail later.

                              Note how we've specified the face and return type of each strand like t=@da, etc.

                              You can see how pure has access to the results of previous strands in the pipeline. Note how we've wrapped pure's argument in a !> because the thread must produce a vase.

                              Next we'll look at strand-input in more detail.

                              <-

                              Fundamentals

                              Input

                              ->

                              Edit this page on GitHub

                              Last modified June 13, 2023