## Where communities thrive

• Join over 1.5M+ people
• Join over 100K+ communities
• Free without limits
##### Activity
Jacob Rothstein
@jbr
Thanks for learning sibilant!
Shivansh Suhane
Thanks for the help!

Actually, if you have a moment, I'd appreciate one more thing. I'm trying to do the following:

if (cur in d){
d[cur]+=1
}
else{
d[cur]=0
}
}

But in sibilant. I'm stuck at:

(each str inputStr
(console.log str)
if (has-key? d str) (d[str]+=1) (d[str]=0)
)

@jbr
Jacob Rothstein
@jbr
(each str input-str
(console.log str)
(if (has-key? d str)
(incr (get d str))
(set d str 0)))
i think you want something like that maybe?
or equivalently,
(each str input-str
(console.log str)
(set d str (if (has-key? d str)
(+ 1 (get d str))
0)))
Shivansh Suhane
Thanks! That makes complete sense
Shivansh Suhane
@jbr Do we have sort or reverse in Sibilant? If not, I could try to contribute sorting! Documentation doesn't mention sort anywhere.
(var x [ 4 1 5 2 3])
(.sort x)
(console.log x) ;; ⇒ [ 1 2 3 4 5 ]
Shivansh Suhane
That's super cool! I was getting weird errors trying it as the syntax was wrong 😅
Jacob Rothstein
@jbr
and for reverse: (.reverse x). to chain reversing and console.logging, check this out: (|> [ 1 2 3 4] .reverse console.log) which is the same as console.log([ 1, 2, 3, 4 ].reverse())
you can also just do (x.sort) or (x.reverse) instead of (.sort x) or (.reverse x), they both work
Shivansh Suhane

Ah interesting, I was doing something like:

(var result Object.entries(d))
(.sort result)
(.reverse result)
(console.log result)

but the chain linking is super cool

Could we sort by the second value in a 2-d array?
Jacob Rothstein
@jbr
yeah, that would be an argument to the Array.prototype.sort function
Shivansh Suhane

Think I understand the JS version:

function comparator(a, b) {
if (a[1] > b[1]) return -1
if (a[1] < b[1]) return 1
return 0
}

myArray = myArray.sort(comparator)

Jacob Rothstein
@jbr
so sorting by the second element in sibilant might look like:
(var x [[0 1] [ 3 5] [ 0 0 ]])
(.sort x (#([a b] [c d])
(if (< b d) -1
(= b d) 0
1)))
(console.log x)
(if i got the logic right)
Shivansh Suhane
That worked like a charm. Thanks!
Jacob Rothstein
@jbr
Shivansh Suhane
🥳🥳
motivate
@motivate:matrix.org
[m]
regarding sorting this
I want my count to be printed in descending order
but I am not able to do it in sibilant
I am using this
code
from above reference
Jacob Rothstein
@jbr
could you reformat that as one code block? it's hard to tell what you're doing
I don't see anything about sorting in that code block
motivate
@motivate:matrix.org
[m]
it is giving me output for input: (var strings (list 'hello 'hi 'hi 'hello 'hi))
output: { hello: 2, hi: 3 }
undefined
@jbr
@jbr: I have not done sorting in this code
Jacob Rothstein
@jbr
could you reformat the code into a single block? i can't really tell what's going on
motivate
@motivate:matrix.org
[m]
I want my output to be {hi:3, hello:2} in descending order
okay let me send a picture to you
Jacob Rothstein
@jbr
please just use 
motivate
@motivate:matrix.org
[m]
Nope I am not able to do it
Jacob Rothstein
@jbr
 is different from '''
motivate
@motivate:matrix.org
[m]
ohh okay sorry
Jacob Rothstein
@jbr
there's a lot going on here. console.log returns undefined
so arrayToHash returns undefined
motivate
@motivate:matrix.org
[m]
Yes I want to remove this undefined
and I want my array to be printed
with the count of strings