Urbit Docs
  • What is Urbit?
  • Get on Urbit
  • Build on Urbit
    • Contents
    • Environment Setup
    • Hoon School
      • 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
      • 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)
      • 1. Types
      • 2. Agent
      • 3. JSON
      • 4. Marks
      • 5. Eyre
      • 6. React app setup
      • 7. React app logic
      • 8. Desk and glob
      • 9. Summary
    • Core Academy
      • 1. Evaluating Nock
      • 2. Building Hoon
      • 3. The Core Stack
      • 4. Arvo I: The Main Sequence
      • 5. Arvo II: The Boot Sequence
      • 6. Vere I: u3 and the Serf
      • 7. Vere II: The Loom
      • 8. Vanes I: Behn, Dill, Kahn, Lick
      • 9. Vanes II: Ames
      • 10. Vanes III: Eyre, Iris
      • 11. Vanes IV: Clay
      • 12. Vanes V: Gall and Userspace
      • 13. Vanes VI: Khan, Lick
      • 14. Vanes VII: Jael, Azimuth
    • Runtime
      • U3
      • Conn.c Guide
      • How to Write a Jet
      • API Overview by Prefix
      • C in Urbit
      • Cryptography
      • Land of Nouns
    • Tools
      • Useful Links
      • JS Libraries
        • HTTP API
      • Docs App
        • File Format
        • Index File
        • Suggested Structure
    • Userspace
      • Command-Line App Tutorial
      • Remote Scry
      • Unit Tests
      • Software Distribution
        • Software Distribution Guide
        • Docket File
        • Glob
      • Examples
        • Building a CLI App
        • Debugging Wrapper
        • Host a Website
        • Serving a JS Game
        • Ship Monitoring
        • Styled Text
  • Urbit ID
    • What is Urbit ID?
    • Azimuth Data Flow
    • Life and Rift
    • Urbit HD Wallet
    • Advanced Azimuth Tools
    • Custom Roller Tutorial
    • Azimuth.eth Reference
    • Ecliptic.eth Reference
    • Layer 2
      • L2 Actions
      • L2 Rollers
      • L2 Roller HTTP RPC-API
      • L2 Transaction Format
  • Urbit OS
    • What is Urbit OS?
    • Base
      • Hood
      • Threads
        • Basics Tutorial
          • Bind
          • Fundamentals
          • Input
          • Output
          • Summary
        • HTTP API Guide
        • Spider API Reference
        • Strandio Reference
        • Examples
          • Child Thread
          • Fetch JSON
          • Gall
            • Poke Thread
            • Start Thread
            • Stop Thread
            • Take Facts
            • Take Result
          • Main-loop
          • Poke Agent
          • Scry
          • Take Fact
    • Kernel
      • Arvo
        • Cryptography
        • Move Trace
        • Scries
        • Subscriptions
      • Ames
        • Ames API Reference
        • Ames Cryptography
        • Ames Data Types
        • Ames Scry Reference
      • Behn
        • Behn API Reference
        • Behn Examples
        • Behn Scry Reference
      • Clay
        • Clay API Reference
        • Clay Architecture
        • Clay Data Types
        • Clay Examples
        • Clay Scry Reference
        • Filesystem Hierarchy
        • Marks
          • Mark Examples
          • Using Marks
          • Writing Marks
        • Using Clay
      • Dill
        • Dill API Reference
        • Dill Data Types
        • Dill Scry Reference
      • Eyre
        • EAuth
        • Eyre Data Types
        • Eyre External API
        • Eyre Internal API
        • Eyre Scry Reference
        • Low-Level Eyre Guide
        • Noun channels
      • Gall
        • Gall API Reference
        • Gall Data Types
        • Gall Scry Reference
      • Iris
        • Iris API Reference
        • Iris Data Types
        • Iris Example
      • Jael
        • Jael API Reference
        • Jael Data Types
        • Jael Examples
        • Jael Scry Reference
      • Khan
        • Khan API Reference
        • Khan Data Types
        • Khan Example
      • Lick
        • Lick API Reference
        • Lick Guide
        • Lick Examples
        • Lick Scry Reference
  • Hoon
    • Why Hoon?
    • Advanced Types
    • Arvo
    • Auras
    • Basic Types
    • Cheat Sheet
    • Cryptography
    • Examples
      • ABC Blocks
      • Competitive Programming
      • Emirp
      • Gleichniszahlenreihe
      • Islands
      • Luhn Number
      • Minimum Path Sum
      • Phone Letters
      • Restore IP
      • Rhonda Numbers
      • Roman Numerals
      • Solitaire Cipher
      • Water Towers
    • Generators
    • Hoon Errors
    • Hoon Style Guide
    • Implementing an Aura
    • Irregular forms
    • JSON
    • Limbs and wings
      • Limbs
      • Wings
    • Mips (Maps of Maps)
    • Parsing Text
    • Runes
      • | bar · Cores
      • $ buc · Structures
      • % cen · Calls
      • : col · Cells
      • . dot · Nock
      • / fas · Imports
      • ^ ket · Casts
      • + lus · Arms
      • ; mic · Make
      • ~ sig · Hints
      • = tis · Subject
      • ? wut · Conditionals
      • ! zap · Wild
      • Constants (Atoms and Strings)
      • --, == · Terminators
    • Sail (HTML)
    • Serialization
    • Sets
    • Standard Library
      • 1a: Basic Arithmetic
      • 1b: Tree Addressing
      • 1c: Molds and Mold-Builders
      • 2a: Unit Logic
      • 2b: List Logic
      • 2c: Bit Arithmetic
      • 2d: Bit Logic
      • 2e: Insecure Hashing
      • 2f: Noun Ordering
      • 2g: Unsigned Powers
      • 2h: Set Logic
      • 2i: Map Logic
      • 2j: Jar and Jug Logic
      • 2k: Queue Logic
      • 2l: Container from Container
      • 2m: Container from Noun
      • 2n: Functional Hacks
      • 2o: Normalizing Containers
      • 2p: Serialization
      • 2q: Molds and Mold-Builders
      • 3a: Modular and Signed Ints
      • 3b: Floating Point
      • 3c: Urbit Time
      • 3d: SHA Hash Family
      • 3e: AES encryption (Removed)
      • 3f: Scrambling
      • 3g: Molds and Mold-Builders
      • 4a: Exotic Bases
      • 4b: Text Processing
      • 4c: Tank Printer
      • 4d: Parsing (Tracing)
      • 4e: Parsing (Combinators)
      • 4f: Parsing (Rule-Builders)
      • 4g: Parsing (Outside Caller)
      • 4h: Parsing (ASCII Glyphs)
      • 4i: Parsing (Useful Idioms)
      • 4j: Parsing (Bases and Base Digits)
      • 4k: Atom Printing
      • 4l: Atom Parsing
      • 4m: Formatting Functions
      • 4n: Virtualization
      • 4o: Molds
      • 5a: Compiler Utilities
      • 5b: Macro Expansion
      • 5c: Compiler Backend & Prettyprinter
      • 5d: Parser
      • 5e: Molds and mold builders
      • 5f: Profiling support
    • Strings
    • The Engine Pattern
    • Udon (Markdown-esque)
    • Vases
    • Zuse
      • 2d(1-5): To JSON, Wains
      • 2d(6): From JSON
      • 2d(7): From JSON (unit)
      • 2e(2-3): Print & Parse JSON
      • 2m: Ordered Maps
  • Nock
    • What is Nock?
    • Decrement
    • Definition
    • Fast Hints and Jets
    • Implementations
    • Specification
  • User Manual
    • Contents
    • Running Urbit
      • Cloud Hosting
      • Home Servers
      • Runtime Reference
      • Self-hosting S3 Storage with MinIO
    • Urbit ID
      • Bridge Troubleshooting
      • Creating an Invite Pool
      • Get an Urbit ID
      • Guide to Factory Resets
      • HD Wallet (Master Ticket)
      • Layer 2 for planets
      • Layer 2 for stars
      • Proxies
      • Using Bridge
    • Urbit OS
      • Basics
      • Configuring S3 Storage
      • Dojo Tools
      • Filesystem
      • Shell
      • Ship Troubleshooting
      • Star and Galaxy Operations
      • Updates
Powered by GitBook

GitHub

  • Urbit ID
  • Urbit OS
  • Runtime

Resources

  • YouTube
  • Whitepaper
  • Awesome Urbit

Contact

  • X
  • Email
  • Gather
On this page
  • +by
  • ++all:by
  • ++any:by
  • ++apt:by
  • ++bif:by
  • ++del:by
  • ++dif:by
  • ++dig:by
  • ++gas:by
  • ++get:by
  • ++got:by
  • ++gut:by
  • ++has:by
  • ++int:by
  • ++jab:by
  • ++key:by
  • ++mar:by
  • ++put:by
  • ++rep:by
  • ++rib:by
  • ++run:by
  • ++tap:by
  • ++uni:by
  • ++uno:by
  • ++urn:by
  • ++wyt:by
  • ++val:by
Edit on GitHub
  1. Hoon
  2. Standard Library

2i: Map Logic

+by

Map operations

Container arm for map operation arms. A map is a set of key-value pairs. The contained arms inherit its sample map, a.

Accepts

a is a map.

Source

++  by
  ~/  %by
  =|  a=(tree (pair))  ::  (map)
  =*  node  ?>(?=(^ a) n.a)
  |@

Examples

> ~(. by (malt (limo ~[a+1 b+2 c+3])))
< 27.jus
  [   a
    ?(
      %~
      [ n=[?(p=%a p=%b p=%c) q=@ud]
        l=nlr([p=?(%a %b %c) q=@ud])
        r=nlr([p=?(%a %b %c) q=@ud])
      ]
    )
    <123.zao 46.hgz 1.pnw %140>
  ]
>

++all:by

Logical AND

Computes the logical AND on the results of slamming every element in map a with gate b.

Accepts

a is a map, and is the sample of +by.

b is a gate.

Produces

A flag.

Source

++  all
  ~/  %all
  |*  b=$-(* ?)
  |-  ^-  ?
  ?~  a
    &
  ?&((b q.n.a) $(a l.a) $(a r.a))

Examples

> =a (malt (limo ~[a+1 b+[2 3]]))

> (~(all by a) |=(a=* ?@(a & |)))
%.n
> =a (malt (limo ~[a+1 b+2 c+3 d+4 e+5]))

> (~(all by a) |=(a=@ (lte a 6)))
%.y

> (~(all by a) |=(a=@ (lte a 4)))
%.n

++any:by

Logical OR

Computes the logical OR on the results of slamming every element with gate b.

Accepts

a is a map, and is the sample of +by.

b is a wet gate.

Produces

A flag.

Source

++  any
  ~/  %any
  |*  b=$-(* ?)
  |-  ^-  ?
  ?~  a
    |
  ?|((b q.n.a) $(a l.a) $(a r.a))

Examples

> =a (malt (limo ~[a+1 b+[2 3]]))

> (~(any by a) |=(a=* ?@(a & |)))
%.y
> =a (malt (limo ~[a+1 b+2 c+3 d+4 e+5]))

> (~(any by a) |=(a=@ (lte a 4)))
%.y

++apt:by

Check correctness

Computes whether a has a correct horizontal order and a correct vertical order, producing a flag.

Accepts

a is a map.

Produces

A flag.

Source

++  apt
  =<  $
  ~/  %apt
  =|  [l=(unit) r=(unit)]
  |.  ^-  ?
  ?~  a   &
  ?&  ?~(l & &((gor p.n.a u.l) !=(p.n.a u.l)))
      ?~(r & &((gor u.r p.n.a) !=(u.r p.n.a)))
      ?~  l.a   &
      &((mor p.n.a p.n.l.a) !=(p.n.a p.n.l.a) $(a l.a, l `p.n.a))
      ?~  r.a   &
      &((mor p.n.a p.n.r.a) !=(p.n.a p.n.r.a) $(a r.a, r `p.n.a))
  ==

Examples

> =a (malt `(list [@tas @])`~[a+1 b+2 c+3 d+4 e+5])

> ~(apt by a)
%.y

> =z ?~(a ~ a(p.n `@tas`%z))

> z
[n=[p=%z q=2] l={[p=%e q=5]} r={[p=%d q=4] [p=%a q=1] [p=%c q=3]}]

> ~(apt by z)
%.n

Discussion

See section 2f for more information on noun ordering.

++bif:by

Bifurcate

Splits map a into two maps l and r, which contain the items either side of key b with value c but not including b-c.

Accepts

a is a map, and is the sample of +by.

b is a noun.

c is a noun.

Produces

A cell of two maps.

Source

++  bif
  ~/  %bif
  |*  [b=* c=*]
  ^+  [l=a r=a]
  =<  +
  |-  ^+  a
  ?~  a
    [[b c] ~ ~]
  ?:  =(b p.n.a)
    ?:  =(c q.n.a)
      a
    a(n [b c])
  ?:  (gor b p.n.a)
    =+  d=$(a l.a)
    ?>  ?=(^ d)
    d(r a(l r.d))
  =+  d=$(a r.a)
  ?>  ?=(^ d)
  d(l a(r l.d))

Examples

> =a (malt `(list [@tas @])`~[a+1 b+2 c+3 d+4 e+5])

> (~(bif by a) b+2)
[l=[n=[p=%e q=5] l=~ r=~] r=[n=[p=%d q=4] l=~ r=[n=[p=%c q=3] l={[p=%a q=1]} r={}]]]

> `[(map @tas @) (map @tas @)]`(~(bif by a) b+2)
[{[p=%e q=5]} {[p=%d q=4] [p=%a q=1] [p=%c q=3]}]

Discussion

Note that maps are horizontally ordered by the mug hash of their keys and vertically ordered by the double-mug hash of their keys. This means bifurcating the map (malt ~[10^10 20^20 30^30 40^40 50^50]) at 30^30 will not produce [{10^10 20^20} {40^40 50^50}], but rather [{20^20} {10^10 40^40 50^50}] due to the tree structure resulting from their mug hashes.


++del:by

Delete

Produces map a with the element located at key b removed.

Accepts

a is a map, and is the sample of +by.

b is a key as a noun.

Produces

A map.

Source

++  del
  ~/  %del
  |*  b=*
  |-  ^+  a
  ?~  a
    ~
  ?.  =(b p.n.a)
    ?:  (gor b p.n.a)
      a(l $(a l.a))
    a(r $(a r.a))
  |-  ^-  [$?(~ _a)]
  ?~  l.a  r.a
  ?~  r.a  l.a
  ?:  (mor p.n.l.a p.n.r.a)
    l.a(r $(l.a r.l.a))
  r.a(l $(r.a l.r.a))

Examples

> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3 d+4]))
> a
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> `(map @tas @)`(~(del by a) %z)
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> `(map @tas @)`(~(del by a) %b)
{[p=%d q=4] [p=%a q=1] [p=%c q=3]}

++dif:by

Difference

Computes the difference between a and b, producing the map of key-value pairs in a whose keys are not in b.

Accepts

a is a map, and is the sample of +by.

b is a map.

Produces

A map.

Source

++  dif
  ~/  %dif
  =+  b=a
  |@
  ++  $
    |-  ^+  a
    ?~  b
      a
    =+  c=(bif p.n.b q.n.b)
    ?>  ?=(^ c)
    =+  d=$(a l.c, b l.b)
    =+  e=$(a r.c, b r.b)
    |-  ^-  [$?(~ _a)]
    ?~  d  e
    ?~  e  d
    ?:  (mor p.n.d p.n.e)
      d(r $(d r.d))
    e(l $(e l.e))
  --

Examples

> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3 d+4]))
> =b `(map @tas @)`(malt (limo ~[c+3 d+4 e+5 f+6]))
> a
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}
> b
{[p=%e q=5] [p=%d q=4] [p=%f q=6] [p=%c q=3]}

> `(map @tas @)`(~(dif by a) b)
{[p=%b q=2] [p=%a q=1]}
> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3 d+4]))
> =b `(map @tas @)`(malt (limo ~[a+2 e+4 f+5]))
> a
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}
> b
{[p=%e q=4] [p=%f q=5] [p=%a q=2]}

> `(map @tas @)`(~(dif by a) b)
{[p=%b q=2] [p=%d q=4] [p=%c q=3]}

Discussion

This only compares keys, so if both maps contain the same key with different values, that key-value pair is not considered a difference and will not be included in the resulting map.


++dig:by

Address of key

Produce the address of key b within map a.

Accepts

a is a map, and is the sample of +by.

b is a key as a noun.

Produces

A unit.

Source

++  dig
  |=  b=*
  =+  c=1
  |-  ^-  (unit @)
  ?~  a  ~
  ?:  =(b p.n.a)  [~ u=(peg c 2)]
  ?:  (gor b p.n.a)
    $(a l.a, c (peg c 6))
  $(a r.a, c (peg c 7))

Examples

> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3 d+4]))

> (~(dig by a) %a)
[~ 252]

> (~(dig by a) %b)
[~ 2]

> (~(dig by a) %e)
~

++gas:by

Merge.

Like Clojure's merge. Insert a list of key-value pairs b into map a. For a key which exists in both a and b, the value is replaced with the value in b.

Accepts

a is a map, and is the sample of +by.

b is a list of cells of key-value nouns p and q.

Produces

A map.

Source

++  gas
  ~/  %gas
  |*  b=(list [p=* q=*])
  =>  .(b `(list _?>(?=(^ a) n.a))`b)
  |-  ^+  a
  ?~  b
    a
  $(b t.b, a (put p.i.b q.i.b))

Examples

> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3 d+4]))
> a
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> `(map @tas @)`(~(gas by a) ~[e+5 f+6 g+7])
{[p=%e q=5] [p=%b q=2] [p=%d q=4] [p=%f q=6] [p=%g q=7] [p=%a q=1] [p=%c q=3]}
> =a `(map @tas @)`(malt (limo ~[a+1 b+2]))
> a
{[p=%b q=2] [p=%a q=1]}

> `(map @tas @)`(~(gas by a) ~[a+100 b+200])
{[p=%b q=200] [p=%a q=100]}
> `(map @tas @)`(~(gas by `(map @tas @)`~) ~[a+100 b+200])
{[p=%b q=200] [p=%a q=100]}

++get:by

Grab unit value

Produce the unit value of the value located at key b within map a.

Accepts

a is a map, and is the sample of +by.

b is a key as a noun.

Produces

A unit.

Source

++  get
  ~/  %get
  |*  b=*
  =>  .(b `_?>(?=(^ a) p.n.a)`b)
  |-  ^-  (unit _?>(?=(^ a) q.n.a))
  ?~  a
    ~
  ?:  =(b p.n.a)
    (some q.n.a)
  ?:  (gor b p.n.a)
    $(a l.a)
  $(a r.a)

Examples

> a
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> (~(get by a) %a)
[~ 1]

> (~(get by a) %b)
[~ 2]

> (~(get by a) %z)
~

++got:by

Assert

Produce the value located at key b within map a. Crash if key b does not exist.

Accepts

a is a map, and is the sample of +by.

b is a key as a noun.

Produces

A noun.

Source

++  got
  |*  b=*
  (need (get b))

Examples

> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3 d+4]))
> a
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> (~(got by a) %a)
1

> (~(got by a) %b)
2

> (~(got by a) %z)
dojo: hoon expression failed

++gut:by

Grab value with default

Produce the value located at key b within map a. Use default value c if key does not exist.

Accepts

a is a map, and is the sample of +by.

b is a key as a noun.

c is a noun.

Produces

A noun.

Source

++  gut
  |*  [b=* c=*]
  (fall (get b) c)

Examples

> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3 d+4]))
> a
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> (~(gut by a) %a 9.999)
1

> (~(gut by a) %b 9.999)
2

> (~(gut by a) %z 9.999)
9.999

++has:by

Key existence check

Checks whether map a contains an element with key b, producing a flag.

Accepts

a is a map, and is the sample of +by.

b is a key as a noun.

Produces

A flag.

Source

++  has
  ~/  %has
  |*  b=*
  !=(~ (get b))

Examples

> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3 d+4]))
> a
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> (~(has by a) %a)
%.y
> (~(has by a) %z)
%.n

++int:by

Intersection

Produces a map of the (key) intersection between two maps of the same type, a and b. If both maps have an identical key that point to different values, the element from map b is used.

Accepts

a is a map, and is the sample of +by.

b is a map.

Produces

A map.

Source

++  int
  ~/  %int
  =+  b=a
  |@
  ++  $
    |-  ^+  a
    ?~  b
      ~
    ?~  a
      ~
    ?:  (mor p.n.a p.n.b)
      ?:  =(p.n.b p.n.a)
        b(l $(a l.a, b l.b), r $(a r.a, b r.b))
      ?:  (gor p.n.b p.n.a)
        %-  uni(a $(a l.a, r.b ~))  $(b r.b)
      %-  uni(a $(a r.a, l.b ~))  $(b l.b)
    ?:  =(p.n.a p.n.b)
      b(l $(b l.b, a l.a), r $(b r.b, a r.a))
    ?:  (gor p.n.a p.n.b)
      %-  uni(a $(b l.b, r.a ~))  $(a r.a)
    %-  uni(a $(b r.b, l.a ~))  $(a l.a)
  --

Examples

> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3 d+4]))
> =b `(map @tas @)`(malt (limo ~[c+3 d+4 e+5 f+6]))
> a
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}
> b
{[p=%e q=5] [p=%d q=4] [p=%f q=6] [p=%c q=3]}

> `(map @tas @)`(~(int by a) b)
{[p=%d q=4] [p=%c q=3]}
> =a `(map @tas @)`(malt (limo ~[a+1 b+2]))
> =b `(map @tas @)`(malt (limo ~[a+100 b+200]))
> a
{[p=%b q=2] [p=%a q=1]}
> b
{[p=%b q=200] [p=%a q=100]}

> `(map @tas @)`(~(int by a) b)
{[p=%b q=200] [p=%a q=100]}

++jab:by

Transform value

Produce map a with the value at key b transformed by gate c.

Accepts

a is a map, and is the sample of +by.

b is a noun, and a key in a.

c is a gate.

Produces

A map.

Source

++  jab
  ~/  %jab
  |*  [key=_?>(?=(^ a) p.n.a) fun=$-(_?>(?=(^ a) q.n.a) _?>(?=(^ a) q.n.a))]
  ^+  a
  ::
  ?~  a  !!
  ::
  ?:  =(key p.n.a)
    a(q.n (fun q.n.a))
  ::
  ?:  (gor key p.n.a)
    a(l $(a l.a))
  ::
  a(r $(a r.a))

Examples

> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3 d+4]))
> a
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> `(map @tas @)`(~(jab by a) %d |=(x=@ (pow x 2)))
{[p=%b q=2] [p=%d q=16] [p=%a q=1] [p=%c q=3]}

> (~(jab by a) %z |=(x=@ (pow x 2)))
dojo: hoon expression failed

> (~(jab by a) %d |=(a=@ [a a]))
-need.?(%~ [n=[p=@tas q=@] l=nlr([p=@tas q=@]) r=nlr([p=@tas q=@])])
-have.[n=[p=@tas q=[@ @]] l=nlr([p=@tas q=@]) r=nlr([p=@tas q=@])]
nest-fail
dojo: hoon expression failed

++key:by

Set of keys

Produces a set of all keys in map a.

Accepts

a is a map, and is the sample of +by.

Produces

A set.

Source

++  key
  =<  $
  ~/  %key
  =+  b=`(set _?>(?=(^ a) p.n.a))`~
  |.  ^+  b
  ?~  a   b
  $(a r.a, b $(a l.a, b (~(put in b) p.n.a)))

Examples

> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3 d+4]))

> ~(key by a)
{%b %d %a %c}

++mar:by

Add with validation

Produces map a with the addition of key-value pair b and c, where the value is a nonempty unit.

Accept a noun and a unit of a noun of the type of the map's keys and values, respectively. Validate that the value is not null and put the pair in the map. If the value is null, delete the key.

Accepts

a is a map, and is the sample of +by.

b is a noun.

c is a unit.

Produces

A map.

Source

++  mar
  |*  [b=* c=(unit *)]
  ?~  c
    (del b)
  (put b u.c)

Examples

> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3 d+4]))
> a
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> `(map @tas @)`(~(mar by a) %e (some 5))
{[p=%e q=5] [p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> `(map @tas @)`(~(mar by a) %a (some 10))
{[p=%b q=2] [p=%d q=4] [p=%a q=10] [p=%c q=3]}

> `(map @tas @)`(~(mar by a) %a ~)
{[p=%b q=2] [p=%d q=4] [p=%c q=3]}

++put:by

Add key-value pair

Produces a with the addition of the key-value pair of b and c.

Accepts

a is a map, and is the sample of +by.

b is a key of the same type as the keys in a.

c is a value of the same type of the values in a.

Produces

A map.

Source

++  put
  ~/  %put
  |*  [b=* c=*]
  |-  ^+  a
  ?~  a
    [[b c] ~ ~]
  ?:  =(b p.n.a)
    ?:  =(c q.n.a)
      a
    a(n [b c])
  ?:  (gor b p.n.a)
    =+  d=$(a l.a)
    ?>  ?=(^ d)
    ?:  (mor p.n.a p.n.d)
      a(l d)
    d(r a(l r.d))
  =+  d=$(a r.a)
  ?>  ?=(^ d)
  ?:  (mor p.n.a p.n.d)
    a(r d)
  d(l a(r l.d))

Examples

> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3]))
> a
{[p=%b q=2] [p=%a q=1] [p=%c q=3]}

> `(map @tas @)`(~(put by a) %d 4)
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> `(map @tas @)`(~(put by a) %a 10)
{[p=%b q=2] [p=%a q=10] [p=%c q=3]}

> (~(put by a) 42 'foo')
mull-grow
mull-nice
-need.?(%~ [n=[p=@tas q=@] l=nlr([p=@tas q=@]) r=nlr([p=@tas q=@])])
-have.[[@ud @t] %~ %~]
nest-fail
dojo: hoon expression failed

++rep:by

Reduce to product

Accumulate elements of map a using gate b, producing a noun.

Accepts

a is a map.

b is a gate.

Produces

A noun.

Source

++  rep
  ~/  %rep
  |*  b=_=>(~ |=([* *] +<+))
  |-
  ?~  a  +<+.b
  $(a r.a, +<+.b $(a l.a, +<+.b (b n.a +<+.b)))

Examples

> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3 d+4]))
> a
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> (~(rep by a) |=([p=[@tas @] q=@] ~&([p q] (add +.p q))))
[[%b 2] 0]
[[%d 4] 2]
[[%c 3] 6]
[[%a 1] 9]
q=10

Discussion

The gate will iteratively be fed a cell whose head is a key-value pair from the map and whose tail is an accumulator, producing the final value of the accumulator.


++rib:by

Transform + product

c is a gate with a sample like [[key value] accumulator] and a product like [accumulator [key value]]. Each key-value pair in map a is passed to c and replaced with the key-value pair c produced. The final value of the accumulator and the modified map are returned. b is the initial value of the accumulator.

Accepts

a is a map, and is the sample of +by.

b is a noun, and is the initial value of the accumulator.

c is a gate.

Produces

A cell of a noun and a map.

Source

++  rib
  |*  [b=* c=gate]
  |-  ^+  [b a]
  ?~  a  [b ~]
  =+  d=(c n.a b)
  =.  n.a  +.d
  =+  e=$(a l.a, b -.d)
  =+  f=$(a r.a, b -.e)
  [-.f a(l +.e, r +.f)]

Examples

In this example, all values less than three are changed to zero, and a list of their keys are produced along with the modified map.

> =a `(map @t @)`(malt ~[['a' 1] ['b' 2] ['c' 3] ['d' 4] ['e' 5]])
> a
{[p='e' q=5] [p='b' q=2] [p='d' q=4] [p='a' q=1] [p='c' q=3]}

> =c |=  [[k=@t v=@] acc=(list @t)]
     ?:  (lth v 3)
       [[k acc] [k 0]]
     [acc [k v]]

> `[(list @t) (map @t @)]`(~(rib by a) *(list @t) c)
[<|a b|> {[p='e' q=5] [p='b' q=0] [p='d' q=4] [p='a' q=0] [p='c' q=3]}]

Discussion

Key-value pairs in the map are transformed in their existing tree location. This means if you change the key, you'd likely produce a map with an incorrect order, so typically you should only change the value.


++run:by

Transform values

Iterates over every value in map a using gate b, producing a map.

Accepts

a is a map, and is the sample of +by.

b is a gate.

Produces

A map.

Source

++  run
  ~/  %run
  |*  b=gate
  |-
  ?~  a  a
  [n=[p=p.n.a q=(b q.n.a)] l=$(a l.a) r=$(a r.a)]

Examples

> =a `(map @t @)`(malt ~[['a' 1] ['b' 2] ['c' 3] ['d' 4] ['e' 5]])

> `(map @t @)`(~(run by a) dec)
{[p='e' q=4] [p='b' q=1] [p='d' q=3] [p='a' q=0] [p='c' q=2]}

++tap:by

Listify pairs

Produces the list of all elements in map a.

Accepts

a is a map, and is the sample of +by.

Produces

A list.

Source

++  tap
  =<  $
  ~/  %tap
  =+  b=`(list _?>(?=(^ a) n.a))`~
  |.  ^+  b
  ?~  a
    b
  $(a r.a, b [n.a $(a l.a)])

Examples

> =a `(map @ @)`(malt ~[[1 1] [2 2] [3 3] [4 4] [5 5]])

> ~(tap by a)
~[[p=4 q=4] [p=3 q=3] [p=2 q=2] [p=1 q=1] [p=5 q=5]]

++uni:by

Union, merge

Produces a map of the union between the keys of a and b. If b shares a key with a, the tuple from b is preserved.

Accepts

a is a map, and is the sample +by.

b is a map.

Produces

A map.

Source

++  uni
  ~/  %uni
  =+  b=a
  |@
  ++  $
    |-  ^+  a
    ?~  b
      a
    ?~  a
      b
    ?:  =(p.n.b p.n.a)
      b(l $(a l.a, b l.b), r $(a r.a, b r.b))
    ?:  (mor p.n.a p.n.b)
      ?:  (gor p.n.b p.n.a)
        $(l.a $(a l.a, r.b ~), b r.b)
      $(r.a $(a r.a, l.b ~), b l.b)
    ?:  (gor p.n.a p.n.b)
      $(l.b $(b l.b, r.a ~), a r.a)
    $(r.b $(b r.b, l.a ~), a l.a)
  --

Examples

> =a `(map @ @)`(malt ~[[1 1] [2 2] [3 3]])
> =b `(map @ @)`(malt ~[[3 300] [4 400] [5 500]])
> a
{[p=1 q=1] [p=2 q=2] [p=3 q=3]}
> b
{[p=5 q=500] [p=3 q=300] [p=4 q=400]}

> `(map @ @)`(~(uni by a) b)
{[p=5 q=500] [p=1 q=1] [p=2 q=2] [p=3 q=300] [p=4 q=400]}

++uno:by

General union

Produces a map of the union between the keys of a and b. If b shares a key with a, gate meg is applied to both and its product is used as the new value of the key in question.

Accepts

a is a map, and is the sample of +by.

b is a map, and is the sample of ~(uno by a).

meg is a gate, and is the sample of (~(uno by a) b).

Produces

A map.

Source

++  uno
  =+  b=a
  |@
  ++  $
    |=  meg=$-([_p:node _q:node _q:node] _q:node)
    |-  ^+  a
    ?~  b
      a
    ?~  a
      b
    ?:  =(p.n.b p.n.a)
      :+  [p.n.a (meg p.n.a q.n.a q.n.b)]
        $(b l.b, a l.a)
      $(b r.b, a r.a)
    ?:  (mor p.n.a p.n.b)
      ?:  (gor p.n.b p.n.a)
        $(l.a $(a l.a, r.b ~), b r.b)
      $(r.a $(a r.a, l.b ~), b l.b)
    ?:  (gor p.n.a p.n.b)
      $(l.b $(b l.b, r.a ~), a r.a)
    $(r.b $(b r.b, l.a ~), a l.a)
  --

Examples

> =a `(map @ @)`(malt ~[[1 1] [2 2] [3 3]])
> =b `(map @ @)`(malt ~[[3 3] [4 4] [5 5]])
> a
{[p=1 q=1] [p=2 q=2] [p=3 q=3]}
> b
{[p=5 q=5] [p=3 q=3] [p=4 q=4]}

> `(map @ @)`((~(uno by a) b) |=([k=@ v=@ w=@] (add v w)))
{[p=5 q=5] [p=1 q=1] [p=2 q=2] [p=3 q=6] [p=4 q=4]}

++urn:by

Turn (with key)

Iterates over every value in map a using gate b, which accepts both the key and the value of each element as its sample.

Accepts

a is a map.

b is a gate that accepts two nouns.

Produces

A map.

Source

++  urn
  ~/  %urn
  |*  b=$-([* *] *)
  |-
  ?~  a  ~
  a(n n.a(q (b p.n.a q.n.a)), l $(a l.a), r $(a r.a))

Examples

> =a `(map @ @)`(malt ~[[1 1] [2 2] [3 3]])
> a
{[p=1 q=1] [p=2 q=2] [p=3 q=3]}

> (~(urn by a) |=([k=@ v=@] (pow v 2)))
{[p=1 q=1] [p=2 q=4] [p=3 q=9]}

++wyt:by

Depth

Produce the size of the tree map a.

Accepts

a is a map, and is the sample of +by.

Produces

An atom.

Source

++  wyt
  =<  $
  ~%  %wyt  +  ~
  |.  ^-  @
  ?~(a 0 +((add $(a l.a) $(a r.a))))

Examples

> =a `(map @ @)`(malt ~[[1 1] [2 2] [3 3]])
> =b `(map @ @)`(malt ~[[1 1] [2 2] [3 3] [4 4] [5 5]])
> a
{[p=1 q=1] [p=2 q=2] [p=3 q=3]}
> b
{[p=5 q=5] [p=1 q=1] [p=2 q=2] [p=3 q=3] [p=4 q=4]}

> ~(wyt by a)
3

> ~(wyt by b)
5

++val:by

List of values

Produces a list of all values in map a.

Accepts

a is a map.

Produces

A list.

Source

++  val
  =+  b=`(list _?>(?=(^ a) q.n.a))`~
  |-  ^+  b
  ?~  a   b
  $(a r.a, b [q.n.a $(a l.a)])

Examples

> =a `(map @t @)`(malt ~[['a' 1] ['b' 2] ['c' 3]])
> a
{[p='b' q=2] [p='a' q=1] [p='c' q=3]}

> ~(val by a)
~[3 1 2]

Previous2h: Set LogicNext2j: Jar and Jug Logic

Last updated 1 day ago