Nim implementation of Ruby tap() method

2024-04-25 Thread demotomohiro
I don't know about Ruby but `tap()` looks like [Statement list expression](https://nim-lang.org/docs/manual.html#statements-and-expressions-statement-list-expression) or [Block expression](https://nim-lang.org/docs/manual.html#statements-and-expressions-block-expression). But they cannot be cha

Nim implementation of Ruby tap() method

2024-04-25 Thread xigoi
Whoops, sorrY, I forgot that templates don’t create a scope.

Nim implementation of Ruby tap() method

2024-04-24 Thread DMisener
Thanks ... looks like I reversed the working and non working version above...

Nim implementation of Ruby tap() method

2024-04-24 Thread janAkali
That's very easy to fix, use a `block` to avoid clash of identifiers: template tapIt*[T](obj: T, code: untyped): T = block: let it {.inject.} = obj code it var num = 123 echo num.tapIt(echo 2 * it) echo num.tapIt(echo 3 * it)

Nim implementation of Ruby tap() method

2024-04-24 Thread DMisener
## Problem: * A second invocation causes error because of **{.inject.}** INim 0.6.1 Nim Compiler Version 2.0.4 [MacOSX: amd64] at /Users/dennismisener/.nimble/bin/nim nim> template tapIt*[T](obj: T, code: untyped): T = block: let it {.inject.} = obj

Nim implementation of Ruby tap() method

2024-04-24 Thread DMisener
This last one is the winner!! It feels more nim'ish and better still gives me an excuse to use the word _anaphoric_ 🙂 Yes I did overcomplicate it -- I used the **var** since I thought I might want to mutate the input... probably a bad practice anyways. Thanks It always so simple **once you

Nim implementation of Ruby tap() method

2024-04-24 Thread DMisener
Would you mind sharing the source code for **debugs**? I'm curious to see how it was done. Thanks

Nim implementation of Ruby tap() method

2024-04-24 Thread xigoi
Here is also an anaphoric version (to borrow Lisp terminology) if you prefer: template tapIt*[T](obj: T, code: untyped): T = let it {.inject.} = obj code obj var num = 123 echo num.tapIt(echo 2 * it) Run

Nim implementation of Ruby tap() method

2024-04-24 Thread juancarlospaco
I use something kinda similar as a macro debugger:

Nim implementation of Ruby tap() method

2024-04-24 Thread xigoi
You overcomplicated it. This works: proc tap*[T](obj: T, code: proc(obj: T)): T = code obj obj let code = proc(i: int) = echo 2*i var num = 123 echo num.tap(code) Run To make it feel more like the Ruby version, you can use an anon

Nim implementation of Ruby tap() method

2024-04-24 Thread DMisener
## I would appreciate a Nim implementation of Ruby's **tap()** method. This is one of those _little helper_ functions I miss from my old Ruby days, where **tap()** : Yields self to the block, and then returns self. The primary purpose of this [method](https://apidock.com/ruby/Object/method) is