Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request for examples in README #3

Open
kaushalmodi opened this issue Jul 17, 2019 · 1 comment
Open

Request for examples in README #3

kaushalmodi opened this issue Jul 17, 2019 · 1 comment

Comments

@kaushalmodi
Copy link

Hello,

I started out trying to translate some lc (which is now deprecated) examples to comp, but I am completely lost where to begin.

There are my lc examples: https://scripter.co/notes/nim/#lc-deprecated

Is it possible to write the equivalent using comp? If so, can you please add few examples to the README?

Thanks.

@kaushalmodi
Copy link
Author

kaushalmodi commented Jul 17, 2019

OK, I mostly understood how to use comp as a replacement of lc: https://scripter.co/notes/nim/#comprehension

But I still cannot figure out how to translate this to comp:

import std/[sugar]

const
  n = 15
let
  numList = 1 .. n
  rightAngleTriangleSides =
    lc[(x, y, z) | (x <- numList,      # lc[EXPR | (ELEM1 <- LIST1,
                    y <- x .. n,       #            ELEM2 <- LIST2,
                    z <- y .. n,       #            ELEM3 <- LIST3,
                    x*x + y*y == z*z), #            COND),
       tuple[a, b, c: int]]            #    SEQ_TYPE]
echo rightAngleTriangleSides

https://play.nim-lang.org/#ix=1OLo


Failed attempt

import std/[strformat, sequtils]
import comprehension

const
  n = 15
let
  numList = 1 .. n
  numSeq = numList.toSeq
  rightAngleTriangleSides: seq[tuple[a, b, c: int]] =
    comp[for idx, sideA in numSeq:
           for sideB in sideA .. n:
             for sideC in sideB .. n:
               if sideA*sideA + sideB*sideB == sideC*sideC:
                 (sideA, sideB, sideC)]
echo &"numList of type {$type(numList)} = {numList}"
echo &"numSeq of type {$type(numSeq)} = {numSeq}"
echo &"rightAngleTriangleSides of type {$type(rightAngleTriangleSides)} (seq[SEQ_TYPE]) = {rightAngleTriangleSides}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant