| 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. |