Skip to content

Instantly share code, notes, and snippets.

@daniellansun
Created November 7, 2020 16:10
Show Gist options
  • Save daniellansun/9d0235a84f28f2c396b4c12cb78304c0 to your computer and use it in GitHub Desktop.
Save daniellansun/9d0235a84f28f2c396b4c12cb78304c0 to your computer and use it in GitHub Desktop.
Generate Multiplication Table with GINQ
GQ {
from v in (
from a in 1..9
innerjoin b in 1..9 on a <= b
select a as f, b as s, "$a * $b = ${a * b}".toString() as r
)
groupby v.s
select max(v.f == 1 ? v.r : '') as v1,
max(v.f == 2 ? v.r : '') as v2,
max(v.f == 3 ? v.r : '') as v3,
max(v.f == 4 ? v.r : '') as v4,
max(v.f == 5 ? v.r : '') as v5,
max(v.f == 6 ? v.r : '') as v6,
max(v.f == 7 ? v.r : '') as v7,
max(v.f == 8 ? v.r : '') as v8,
max(v.f == 9 ? v.r : '') as v9
}
@kayr
Copy link

kayr commented Nov 9, 2020

What do you think if the select statement can be used to create another table in a different way that reduces nesting levels

GQ {

     //step 1
     from a in 1..9
     innerjoin b in 1..9 on a <= b
     select a as f, b as s, "$a * $b = ${a * b}".toString() as r 
     as temporay table v //pseudo code to store the result in "V" variable

    // step 2
    from v
    groupby v.s
    select max(v.f == 1 ? v.r : '') as v1,
           max(v.f == 2 ? v.r : '') as v2,
           max(v.f == 3 ? v.r : '') as v3,
           max(v.f == 4 ? v.r : '') as v4,
           max(v.f == 5 ? v.r : '') as v5,
           max(v.f == 6 ? v.r : '') as v6,
           max(v.f == 7 ? v.r : '') as v7,
           max(v.f == 8 ? v.r : '') as v8,
           max(v.f == 9 ? v.r : '') as v9
     as temporay table c //pseudo code to store the result in "C" variable 

  //step 3
  from c 
  inner join v on .....
}```

@daniellansun
Copy link
Author

@kayr the following code may be what you want:

def vt = GQ {
    from a in 1..9
    innerjoin b in 1..9 on a <= b
    select a as f, b as s, "$a * $b = ${a * b}".toString() as r
}

GQ {
    from v in vt
    groupby v.s
    select max(v.f == 1 ? v.r : '') as v1,
           max(v.f == 2 ? v.r : '') as v2,
           max(v.f == 3 ? v.r : '') as v3,
           max(v.f == 4 ? v.r : '') as v4,
           max(v.f == 5 ? v.r : '') as v5,
           max(v.f == 6 ? v.r : '') as v6,
           max(v.f == 7 ? v.r : '') as v7,
           max(v.f == 8 ? v.r : '') as v8,
           max(v.f == 9 ? v.r : '') as v9
}

@kayr
Copy link

kayr commented Nov 9, 2020

That does it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment