Arc Forumnew | comments | leaders | submitlogin
Show Arc: seamless interop with racket
3 points by shawn 14 days ago | 8 comments
https://github.com/arclanguage/anarki/pull/161

(More of a demo than a real proposal.)

  arc> (sequence->list "foo")
  '(#\f #\o #\o)

  arc> (sequence->list (in-range 10 0 -0.5))
  '(10
    9.5
    9.0
    8.5
    8.0
    7.5
    7.0
    6.5
    6.0
    5.5
    5.0
    4.5
    4.0
    3.5
    3.0
    2.5
    2.0
    1.5
    1.0
    0.5)

  arc> (each n (in-range 10 0 -0.5) (out n (expt n 2)))
  '((10 100)
    (9.5 90.25)
    (9.0 81.0)
    (8.5 72.25)
    (8.0 64.0)
    (7.5 56.25)
    (7.0 49.0)
    (6.5 42.25)
    (6.0 36.0)
    (5.5 30.25)
    (5.0 25.0)
    (4.5 20.25)
    (4.0 16.0)
    (3.5 12.25)
    (3.0 9.0)
    (2.5 6.25)
    (2.0 4.0)
    (1.5 2.25)
    (1.0 1.0)
    (0.5 0.25))

  ; use racket's sort
  arc> (|sort| '(a b "foo" 21) (compare > string))
  '("foo" b a 21)

  arc> (|sort| '(a b "foo" 21) (compare < string))
  '(21 a b "foo")

  arc> (sequence->list (in-producer (thunk (bytes-ref (crypto-random-bytes 1) 0)) (%do [< _ 42])))
  '(56 119)

  arc> (sequence->list (in-producer (thunk (bytes-ref (crypto-random-bytes 1) 0)) (%do [< _ 42])))
  '(192 163 45 163 79 184 218 105 67 67 240 228)

  arc> (sequence->list (in-producer (thunk (bytes-ref (crypto-random-bytes 1) 0)) (%do [< _ 42])))
  '(189)

  arc> (sequence->list (in-producer (thunk (bytes-ref (crypto-random-bytes 1) 0)) (%do [< _ 42])))
  '(141 148 243)

  arc> (define (1+ (n 0)) (+ n 1))

  arc> (1+ 21)
  22

  arc> (1+)
  1

  arc> (1+:1+:1+ 21)
  24




2 points by rocketnia 13 days ago | link

I've left a code review at: https://github.com/arclanguage/anarki/pull/161#pullrequestre...

Basically, I think it's a bad idea to change `ac` into something which sometimes compiles Arc code and sometimes does something more like code-walking over s-expressions. I think Anarki's existing "$ ... unquote" syntax already serves this purpose and uses the same kind of code-walking but does so with a better separation of concerns.

Moreover, the way you're taking out the |foo| syntax so you can redefine it to be a variant of $ seems like a net loss.

(Some of my other comments on that review are less fundamental objections: Style nitpicks, observations of bugs, or wild ideas that I don't really expect anyone to act on in the short term.)

reply

3 points by krapp 12 days ago | link

>or wild ideas that I don't really expect anyone to act on in the short term

Those are the best kind of ideas!

reply

3 points by shawn 14 days ago | link

Basically, arc shares a global namespace with racket. (def foo (x) (+ x 1)) results in a function named foo, not _foo.

If an expression starts with a symbol bound to a racket syntax transformer, then the arc compiler switches to "racket-style" output.

  (begin "everything in here is racket code...")
If you want to switch back to arc, you can use (%do ...)

  arc> (begin (require racket) (%do (+ "foo" 42)))
  "foo42"
The last change is that pairwise expressions like (< 1 2) now return #t or #f, not 't or '(). Meaning you can pass arc predicates like `even` into racket functions that expect predicates.

It's pretty convenient to call any racket function without worrying about interop.

reply

2 points by i4cu 13 days ago | link

This is awesome. :)

> If you want to switch back to arc, you can use (%do ...)

Personally I would prefer '.arc' or '%arc':

  arc> (begin (require racket) (.arc (+ "foo" 42)))

  "foo42"
Yeah it's one more char, but I think it makes the code more explicit, understandable and also extendable (i.e. '.racket' also becomes an option too - not that it's needed).

reply

2 points by krapp 13 days ago | link

>(i.e. '.racket' also becomes an option too - not that it's needed).

.arc / .racket (or .rkt) seems more intuitive than .arc / $

We could also keep the dollar sign in both cases, which I prefer aesthetically, because being familiar with javascript and C type languages, seeing a dot alone like that just seems weird.

reply

3 points by i4cu 13 days ago | link

yeah but the dot reads to me like a file extension so I immediately get it.

$arc would be ok too. I can get behind that :)

reply

2 points by shawn 13 days ago | link

Good point! (%arc ...) and (%rkt ...) already work, actually. (I couldn't decide between %arc vs %do and %rkt vs %scm)

reply

2 points by akkartik 13 days ago | link

I wonder if it's possible to end up with a list ending in #f rather than nil.

reply