Arc Forumnew | comments | leaders | submitlogin
Pipe operator
2 points by tokipin 5693 days ago | 7 comments
a pipe operator could be convenient. it would turn this:

  a >> b
into this:

  (b a)
it's left-associative, so that:

  a >> b >> c
becomes:

  (c (b a))
thanks to Arc's lightweight lambda syntax this would allow for a nice version of the anaphoric sequential patterns:

  8
  >> [exp _ 5]
  >> [take-away _ 'monkeys]
  >> [prn _ "monkeys left"]
with the benefit that the piping can be done "points-free" where applicable:

  (annoying 'io 'operation)
  >> process-results
  >> errorcheck
  >> [case _
        error (prn "bah")
        success (prn "meh")]


3 points by absz 5692 days ago | link

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 5692 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 5692 days ago | link

Avoiding re-inventing rreduce:

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

-----

3 points by almkglor 5692 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 5692 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])

-----

1 point by rincewind 5692 days ago | link

someting similar as a macro: http://arclanguage.org/item?id=5987

-----

1 point by unami 5692 days ago | link

Great!

-----