Arc Forumnew | comments | leaders | submitlogin
3 points by absz 5932 days ago | link | parent

Here's an implementation of a >> function; you'd have to write

  (>> 8
      [expt _ 5]
      -
      [take-away _ 'monkeys]
      [prn _ "monkeys left"])
instead, though.

  (def >> (parm . args)
    (if (no args)
      parm
      (apply >> ((car args) parm) (cdr args))))


2 points by almkglor 5932 days ago | link

Avoiding apply:

  (def >> (parm . args)
    ((afn (parm args)
       (if (no args)
           parm
           (self ((car args) parm) (cdr args))))
     parm args))

-----

4 points by rincewind 5932 days ago | link

Avoiding re-inventing rreduce:

  (def >> args
     (rreduce (fn (a b) a.b) rev.args))

-----

3 points by almkglor 5932 days ago | link

Avoiding rev:

  (def >> args
    ; throw an error here so that user won't get
    ; mysterious errors in the 'reduce part
    (if (no args)
      (err "'>> requires at least one argument"))
    (reduce (fn (a b) b.a) args))

-----

1 point by tokipin 5932 days ago | link

deeplier pat matched version:

  (def >> (val . (f . rest))
      (if rest
        (apply >> (f val) rest)
        (f val)))
one thing i noticed is that it's easy to lodge prn in for debugging:

  (>> 1
      [* 20 _] prn
      [* 2 _] prn
      [/ _ 8])

-----