## Where communities thrive

• Join over 1.5M+ people
• Join over 100K+ communities
• Free without limits
• Create your own community
##### Activity
• Apr 04 2021 12:26
@dom96 banned @acroobat
• Mar 03 2017 15:52
@dom96 banned @Octopoda7
• Feb 12 2017 23:57
@dom96 banned @zzz125
• Dec 23 2016 19:43
@dom96 banned @Izrab
Kaushal Modi
@kaushalmodi
@mratsim yep :)
but that jumbled order still bugs me
Mamy Ratsimbazafy
@mratsim
uh no, you need to define the proc then call it
Kaushal Modi
@kaushalmodi
it's like you define a proc with the types and input value, but then you pass the proc definition when you actually call it
Miran
@narimiran
why jumbled? you can use this example now not only for plusOne, but for any other int -> int function
Mamy Ratsimbazafy
@mratsim
fn is not the actual proc though, it’s a stub
it’s instantiated at call site
Kaushal Modi
@kaushalmodi
@mratsim hmm.. "but for any other int -> int function" makes sense
Mamy Ratsimbazafy
@mratsim
define the higher order function —> define it’s body —> call it with a function as parameter
Kaushal Modi
@kaushalmodi
thanks
Miran
@narimiran
maybe it would be clearer if you didn't call it plus1, but use2
Kaushal Modi
@kaushalmodi
@narimiran yeah.. I was translating directly from that elisp snippet
but..
still the input arg gets hardcoded in that use2 wrapper
Mamy Ratsimbazafy
@mratsim
@kaushalmodi
import sugar
proc plus1(fn: int -> int, x: int): int = # 1. define the proc param and return types
fn(x)
echo plus1(x => x + 1, 2)

import sugar
proc plus1(x: int): int = # 1. define the proc param and return types
x + 1
echo plus1(2)
Kaushal Modi
@kaushalmodi
ok.. this is better:
import sugar
proc int2int(fn: proc(x: int): int, inp: int): int = # 1. define the proc param and return types
fn(inp) # 3. call the proc with the actual args
echo int2int(x => x + 1, 2) # 2. define what the fn in plus1 should do
hehe
Miran
@narimiran
yes, that is better :)
From IRC (bridge bot)
@FromIRC
<Yardanico> @Araq are there any examples on how to use --gc:regions? Because person in Telegram tells that he just added --gc:regions and he can use GCd code on esp8266
From IRC (bridge bot)
@FromIRC
Ray Imber
@rayman22201
Damn that is cool!
Kaushal Modi
@kaushalmodi
@mratsim @narimiran Building up that example to one more complication level..
import sugar
proc twoInts2Int[T](fn: (int, int) -> int; x, y: T): T =
## Wrapper proc to define the fn type, and to pass its inputs.
fn(x, y)
echo twoInts2Int((x, y) => x + y, 2, 3)
Is there a way to not hard-code the fn type to deal with ints?
Miran
@narimiran
proc twoInts2Int[T](x, y: T, fn: (T, T) -> T): T =
## Wrapper proc to define the fn type, and to pass its inputs.
fn(x, y)
echo twoInts2Int(2.0, 3.5, (x, y) => x + y)
notice the change in the order of parameters
Kaushal Modi
@kaushalmodi
yeah.. was going to ask that
I had the opposite order.. didn't work
@narimiran :+1: Works! Thanks.
Miran
@narimiran
i think the type is inferred from the first parameter, so it has to be something with the known type - anonymous function isn't
Kaushal Modi
@kaushalmodi
Here's the result of all that Q&A: https://scripter.co/notes/nim/#lambda. Thanks @narimiran and @mratsim for your help!
Clay Sweetser
@Varriount
dom96: Where did you here that?
(Regarding Python and :=)
Miran
@narimiran
thanks for the mention @kaushalmodi! it's these small things that make me happy :)
From IRC (bridge bot)
@FromIRC
<Yardanico> @kaushalmodi but don't forget that you don't need => for lambda
<Yardanico> oh I see
Kaushal Modi
@kaushalmodi
Yardanico:.. yes :)
the last line
From IRC (bridge bot)
@FromIRC
<Yardanico> anonymous procedures = lambdas, right?
Kaushal Modi
@kaushalmodi
yes
I just chose to hyperlink the => with #lambda and the other with #anonymous-procedures as the latter is the "official name" from Nim Manual
From IRC (bridge bot)
@FromIRC
<dom96> makes sense :⁠)
Miran
@narimiran
@kaushalmodi the example after the line "Here are few more examples of using => and ->:" won't work for some general type T
because of the signature of anonymous proc
Kaushal Modi
@kaushalmodi
@narimiran You are right.. they are remnants of me trying to make generics work..
I will remove generics from those two int, flt examples
thanks
Miran
@narimiran
no problem :)
btw, if you'll extend your list comprehension part - try to compare the syntax with map(It) for the same examples, to see what is clearer/nicer to you