2n: Functional Hacks

    ++cork

    Compose forward

    Build f such that (f x) .= (b (a x)).

    Accepts

    a is a noun.

    b is a gate.

    Source

        ++  cork  |*([a=_|=(* **) b=gate] (corl b a))
    

    Examples

        > (:(cork dec dec dec) 20)
        17
    
        > =mal (mo (limo a+15 b+23 ~))
        > ((cork ~(got by mal) dec) %a)
        14
    
        > ((cork ~(got by mal) dec) %b)
        22
    

    ++corl

    Compose backward

    Build f such that

    Accepts

    a is a gate.

    b is a noun.

    Source

        ++  corl
          |*  [a=gate b=_|=(* **)]
          =<  +:|.((a (b)))      ::  span check
          |*  c=_+<.b
          (a (b c))
    

    Examples

        > ((corl (lift bex) (slat %ud)) '2')
        [~ 4]
    

    ++curr

    Right curry

    Right-curry a gate, binding the tail of its sample

    Accepts

    a is a gate.

    c is a noun.

    Produces

    A gate.

    Source

        ++  curr
          |*  [a=_|=(^ **) c=*}
          |*  b=_+<+.a
          (a b c)
    

    Examples

        > =tep (curr scan sym)
        > `@t`(tep "asd")
        'asd'
    
        > `@t`(tep "lek-om")
        'lek-om'
    

    ++cury

    Curry left

    Curry a gate, binding the head of its sample

    Accepts

    a is a gate.

    b is a noun.

    Produces

    A gate.

    Source

        ++  cury
          |*  [a=_|=(^ **) b=*}
          |*  c=_+<+.a
          (a b c)
    

    Examples

        > =mol (cury add 2)
        > (mol 4)
        6
    
        > (mol 7)
        9
    

    ++hard

    Force remold

    Demands that a specific type be produced, crashing the program if it is not.

    Source

        ++  hard
          |*  han=gate
          |=  fud=*  ^-  han
          ~_  leaf+"hard"
          =+  gol=(han fud)
          ?>(=(gol fud) gol)
    

    Examples

        > ((hard (list)) (limo [1 2 3 ~]))
        ~[1 2 3]
        > ((hard @) (add 2 2))
        4
        > ((hard @t) (crip "Tape to cord, bro!"))
        'Tape to cord, bro'
        > ((hard tape) (crip "...Tape to cord, bro?..."))
        ! hard
        ! exit
    

    ++soft

    Maybe remold

    Politely requests a specific type to be produced, producing null if it is not.

    Source

        ++  soft
          |*  han=gate
          |=  fud=*  ^-  (unit han)
          =+  gol=(han fud)
          ?.(=(gol fud) ~ [~ gol])
    

    Examples

        > ((soft %4) (add 2 2))
        [~ %4]
    
        > ((soft @) (add 2 2))
        [~ 4]
    
        > ((soft %5) (add 2 2))
        ~
    
        > ((soft @t) (crip "Tape to cord, Woohoo!"))
        [~ 'Tape to cord, Woohoo!']
    
        > ((soft @t) (trip 'Cmon man... Tape to cord? Please?!'))
        ~
    

    Get head

    Produces the head of a cell.

    Accepts

    A cell.

    Produces

    An atom.

    Source

        ++  head  |*(^ ,:+<-)
    

    Examples

        > (head [1 2])
        1
    
        > (head [[1 1] 2])
        [1 1]
    
        > (head "hello")
        'h'
    

    ++same

    Identity

    Produces the same value that it was given.

    Accepts

    A noun.

    Produces

    A noun.

    Source

        ++  same  |*(* +<)
    

    Examples

        > (same [1 2])
        [1 2]
    
        > (same [[1 1] 2])
        [[1 1] 2]
    
        > (same "hello")
        "hello"
    

    ++slog

    Deify printf

    Produces a stack trace if the stack trace isn't null, then produces the other input.

    Accepts

    A tang.

    Produces

    A tang.

    Source

        ++  slog
          =|  pri=@
          |=  a=tang  ^+  same
          ?~(a same ~>(%slog.[pri i.a] $(a t.a)))
    

    Examples

        > =+  result=((slog [%leaf "error-message"]~) [12 13])  [%product result]
        [%product 12 13]
    

    Discussion

    slog is intended to be put in the middle of a bunch of chained function calls that string a piece of data through them, so that an error message will be printed if there's one to print.


    ++mean

    Crash and printf

    Ends the program and produces a tang tracing error message.

    Accepts

    a is a tang.

    Produces

    A crash.

    Source

        ++  mean
          |=  a=tang
          ^+  !!
          ?~  a  !!
          ~_(i.a $(a t.a))
    

    Examples

        > ~>(%mean.[%leaf "fuse-loop"] !!)
        ####
        !! crash
    

    ++tail

    Get tail

    Produces the tail of a cell.

    Accepts

    A cell.

    Produces

    A noun.

    Source

        ++  tail  |*(^ ,:+<+)
    

    Examples

        > (tail [1 2])
        2
    
        > (tail [[1 1] 2])
        2
    
        > (tail "hello")
        "ello"
    

    ++test

    Test for equality

    Checks if a and b are equal, producing a flag.

    Accepts

    a is a noun.

    b is a noun.

    Produces

    A flag.

    Source

        ++  test  |=(^ =(+<- +<+))
    

    Examples

        > (test 1 1)
        %.y
    
        > (test [2 0] 2)
        %.n
    
        > (test "hello" 'hello')
        %.n
    
        > (test "hello" ['h' 'e' 'l' 'l' 'o' ~])
        %.y