Arc Forumnew | comments | leaders | submitlogin
variable 'that to hold value of last expression
3 points by tokipin 6058 days ago | 11 comments
A variable 'that, 'prev, 'last, or whatever, that holds the value of the last expression. For example:

  3 (+ 2 that)
Would be equivalent to:

  (= that 3)
  (= that (+ 2 that))
I'm not saying that's how it has to be implemented, but that it would be semantically equivalent to that.

Generally, I think this would simplify expressions by allowing them to be written in a one-two punch style. For example, in places where you have one complex expression with another complex expression inside it; or where you need to use an expression, simple or complex, in multiple places. From the news.arc code:

  (let i (load-item id)
       (push i (items i!type)))
This would become:

  (load-item id)
  (push that (items that!type))
Using this as a replacement for 'let in multi-form blocks would require 'do:

  (let a 'hi
     (pr a)
     (prn a))
This would be:

  'hi
  (do (pr that)
      (prn that))
At first it seemed to me that requiring 'do with this method was missing the point, but I think it is actually simpler because the variable 'a is gone. 'that is a lot more implicit.

In similar fashion to how values are returned from functions:

  (fn (x)
      ; some stuff that manipulates x
      x)
We could use likewise in this way:

  (let/do
       ; stuff
       ret-expression)
  (use that)

  --
Regarding:

  'hi
  (do (pr that)
      (prn that))
It occured to me that even with 'do, it wouldn't work because (pr that) would assign a new value to 'that. Funnily, a value which would have made this example seem to work. Questions of scoping handoff also pop up here. Perhaps 'those could be a hash, where those.0 would be 'that in the current scope, those.1 would be 'that in the outer scope, and so on:

  'hi
  (do (pr those.1)
      (prn those.1))
Not to pull at straws, but to provide heuristics.

Also, I'm not saying we should get rid of let, just that this mechanism would be nice. I was surprised when searching through the news.arc code that the vast majority of lets only had one inner code block. There were maybe a handful that were multi-form.

It seems to me that let was mostly used to simplify code, rather than to declare local variables. Noob programmers might not be inclined to doing that, but some encouragement wouldn't be bad.

  -- In Iterations --
I was thinking something like a variable 'prev that held the value of the last iteration:

  [map (fn (x) (+ x prev) ) _]
Which would be equivalent to:

  [reduce (fn (prev x) (+ x prev)) _]
But it seems clunky to me. However, 'that would be nil/undefined at the beginning of the function, so to give it some usefulness in the first line it could be given the value of the last iteration at that point. Or it could be given the value of 'that in the calling scope, or perhaps the continuation of the calling point or something.

  -- Implementation --
I don't really know how this would be implemented, other than it would probably need to be deep in the core for efficiency.


3 points by stefano 6057 days ago | link

This seems more suitable for an imperative style of programming, whereas Arc tends to prefer functional style (no side effects), but it's just a matter of personal taste.

-----

3 points by slashcom 6057 days ago | link

Regardless, it's extremely useful in the REPL. Instead of inlining the previous expression, you can just use 'that.

-----

3 points by sacado 6058 days ago | link

Is that what you want ?

  3 (+ 2 ^)

-----

2 points by lojic 6058 days ago | link

Eh?

  arc> 3 (+ 2 ^)
  3
  arc> Error: "reference to undefined identifier: _^"

-----

1 point by sacado 6058 days ago | link

It works on anarki... I don't know if it is on the official arc2, but you can find the code in anarki and implement it easily...

-----

4 points by lojic 6057 days ago | link

Maybe someone should take the news.arc code and start a new anarki forum. I guess we can rename this forum 'Arc Classic' ;)

-----

1 point by tokipin 6058 days ago | link

is there a place to get the code for anarki without using my email? i'm curious about the implementation

-----

5 points by kens2 6058 days ago | link

See my article http://arcfn.com/2008/02/git-and-anarki-arc-repository-brief... for information on how to access anarki, in particular "Browsing Anarki without git".

-----

2 points by sjs 6057 days ago | link

Have you tried using that? It's in arc2 already.

-----

1 point by tokipin 6057 days ago | link

wow lol. same name and everything. i guess it's not such an uncommon concept. i think i got the idea from anaphoric macros

thanks for pointing it out

[edit]it looks like there is only one global 'that, so it is unusable in functions. it seems more of a REPL mechanism at the moment. i guess to implement it in the spec would require wrapping things in (let that ...), or having it in the core

-----

1 point by Jesin 6056 days ago | link

Ugh. I think I'm going to vote no on this one. When's the last time you tried to read Perl?

-----