Thread-safe cache

2020-06-02 Thread jasonfi
Is there a structure or library that I can use for a thread-safe cache? Keys 
will be strings.


Re: Structure of a web project

2020-06-02 Thread jackhftang
IMO, there is no need to think too much on the file organization, you will find 
how to organize them when the code size grow. One little trick I found useful 
is to add a --path to nim.cfg and create a common.nim that export frequently 
used packages. So that I can easily import packages with import common and less 
code to change when moving/renaming files.

File structure:


- src
  - core
 - foo.nim
 - bar.nim
 - common.nim
- nim.cfg


Run

nim.cfg:


--path:"src/core"


Run

common.nim 


import foo, bar
export foo, bar


Run


Re: Lambda syntax is awkward

2020-06-02 Thread jackhftang
Another technique also seen in std lib.


template twiceIt(input, body: untyped): untyped =
  type T = typeof(input)
  let fn = proc(n: T): T =
let it {.inject.} = n
`body`
  fn(fn(input))


Run


let y = twiceIt 10:
  echo "Gratuitous echo!"
  return it * it


Run


Re: Lambda syntax is awkward

2020-06-02 Thread juancarlospaco
Python lambda is single line only. :) 


Re: Structure of a web project

2020-06-02 Thread Ingon
Just realized that the forum actually shares code, but the shared code is 
defined in the `frontend` module and imported by the main/root module. Maybe 
this is a possible answer, but doesn't seem completely intuitive to me.


Re: Lambda syntax is awkward

2020-06-02 Thread dom96
I wouldn't depend on the do notation. The reason it's experimental is that it 
doesn't fit the language and may be removed entirely eventually.

Personally, I don't think this is too verbose:


let fourthPower = twice(10,
  proc(n: int): int =
echo "Gratuitous echo!"
return n * n
)


Run

It would look very similar in JS. In Python it would simply not be possible.


Structure of a web project

2020-06-02 Thread Ingon
I'm looking for ways/best practices on how to structure a dynamic web site I'm 
planning to do with nim. Looks like I'll be using `jester` for the backend, 
with `karax` for the frontend. Ideally they would live in the same repo and 
share some of the code, for instance the types. So, I'm looking for something 
like: 


- project root
  - core
  - be
  - fe


Run

where both `be` and `fe` would import `core`.

I'm having troubles organizing this using `nimble`. I looked at the forum's 
repository, but it seems like it treats the frontend as a special case.

Any advise?


Re: Lambda syntax is awkward

2020-06-02 Thread treeform
I actually prefer the "original problem" syntax. It's only like couple of 
characters of extra typing. The do, -> and => feel less clear.


Re: Lambda syntax is awkward

2020-06-02 Thread juancarlospaco
[https://github.com/Yardanico/nimpylib/blob/master/src/pylib.nim#L161](https://github.com/Yardanico/nimpylib/blob/master/src/pylib.nim#L161)


Re: Lambda syntax is awkward

2020-06-02 Thread akavel
Hm, not yet there, but arguably somewhat closer with `auto`:


let plusTwo = twice(10) do (n:auto) -> auto:
  echo "Another gratuitious echo"
  n + 1


Run

Maybe this is close enough that now it could be wrapped in some macro to 
shorten even more?


Re: Lambda syntax is awkward

2020-06-02 Thread snej
Oh! I tried using `block` and wondered why it didn't work ... I didn't realize 
parens were necessary.

The `do` notation seems an improvement; it resolves two of the three complaints 
I gave above. If it had better type-inference I'd love it. Maybe that's why 
it's still experimental?

e.g. what I'd like to see is `let plusTwo = twice(10) do (n):`


Re: Uncle Bob - one syntax to rule them all?

2020-06-02 Thread exelotl
Ironically I don't think Uncle Bob would like Nim at all, given our general 
preferance towards straightforward "get shit done" procedural code. For example 
in [this video](https://youtu.be/IRTfhkiAqPw?t=1179) the author critiques some 
of Bob's code and rewrites it in a simple procedural style. The resulting code 
is far more like what I would expect to see in a Nim codebase.


Re: Lambda syntax is awkward

2020-06-02 Thread exelotl
There are some solutions, but all a little awkward.

1\. ["do" 
notation](https://nim-lang.org/docs/manual_experimental.html#do-notation), 
which I believe was moved to _experimental_ just before Nim 1.0, therefore 
might be changed or scrapped in a future Nim version.

Personally I think it's even harder to read than the plain `proc` version. 


let plusTwo = twice(10) do (n:int) -> int:
  echo "Another gratuitious echo"
  n + 1


Run

2\. Sugar module with a [statement list 
expression](https://nim-lang.org/docs/manual.html#statements-and-expressions-statement-list-expression)
 where the last expression in the brackets is treated as the value.

Not idiomatic, wouldn't recommend. 


let plusTwo2 = twice(10, n => (
  echo "Another gratuitious echo";
  n + 1
))


Run

3\. Sugar module with a [block 
expression](https://nim-lang.org/docs/manual.html#statements-and-expressions-block-expression).

I almost like this one! Unfortunately if you remove the brackets, you get " 
_Error: expression expected, but found 'keyword block'_"


let plusTwo3 = twice(10, n => (block:
  echo "Another gratuitious echo"
  n + 1
))


Run

Does anyone know, is this a syntax limitation, or could the `=>` operator be 
modified to allow a block expression without brackets? 


Lambda syntax is awkward

2020-06-02 Thread snej
I'm finding Nim's syntax for calling lambdas awkward and verbose.


proc twice[T](input: T, fn: proc(t: T):T):T =
  fn(fn(input))

let fourthPower = twice(10, proc(n: int): int =
  echo "Gratuitous echo!"
  return n * n
)


Run

  * I have to declare parameter and return types. Even C++ lets me omit the 
return type!
  * The nested proc is still inside the outer proc's parentheses, so that has 
to be closed afterwards.
  * And a nitpick: the initial line ends with `=` not `:`, unlike all other 
statements that introduce nested blocks in a proc.



**I 'm aware of the ``sugar`` module.** The `=>` macro makes this a lot better, 
but it only seems to work with single-line lambdas.


let plusTwo = twice(10, (n) => n + 1)


Run

Every attempt I've made to use it in a multi-line lambda fails with syntax 
errors:


let plusTwo = twice(10, (n) =>
  echo "Another gratuitious echo"
  n + 1)  # Error: expected: ')', but got: 'n'


Run

I like that templates and macros can be called with a block as a final 
parameter, but that feature is limited in that the block can't take any 
parameters and can't return a value.

Is there something I've missed? Or are there proposals to improve this in the 
future?

—Jens


Re: Uncle Bob - one syntax to rule them all?

2020-06-02 Thread zulu
> Nim's syntax seems very reasonable. I would use it as-is if there were just a 
> few more libraries like a SQL Server DB driver, XLSX file API. Maybe a few 
> more but nothing off the top of my head.

There are but they are 1 man projects.


Re: Uncle Bob - one syntax to rule them all?

2020-06-02 Thread mratsim
Just like a carpenter has a hammer, screwdrivers, tapes, wrenches, pliers, 
drills, saws for different purposes, developing is not about trying to hammer 
nails with a Swiss Army Knife but choosing the right tool for the right 
purposes.