by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Jackie S
@eikkaj
thanks! sorry for the dumb q
Jacob Rothstein
@jbr
no worries, no dumb questions! thanks for learning sibilant!
Mathäus
@m3tti
hey guys who is using sibilant in production
and how is it??
i just found out about it
powerserg19
@powerserg19

Hello I am very new to Sibilant. I have this function and want to access the key value pair and update the value of the object. So far i can add to the object but cannot get the value (is a number) and update it. Here is what i have so far. After the "has it" i want to get the value and increment it. When i set it for the first time i want to set the value to 1
Thanks for your help

(def printstrs(arr)
    (var myobj {})        
    (each item arr  
       (if (has-key? myobj item) (console.log "has it"  )
         (set myobj item 1 (console.log "adding" item))
       )
    )

)

Jacob Rothstein
@jbr
i think the issue is just that the console.log is inside of the set call
(def printstrs (arr)
    (var myobj {})        
    (each item arr  
       (if (has-key? myobj item) 
          (console.log "has it")
         (do
           (set myobj item 1)
           (console.log "adding" item)))))
powerserg19
@powerserg19

Hello Helpers of Sibilant

I have a function that takes in a string array and returns a hash to the caller. The problem is I am not doing it right :)
This is what I have
thanks again

(def caller(arr)
   (var stringHash arrayToHash(arr)) 
)
(def arrayToHash(arr)

    (var stringHash (hash))
    (each item arr
       (if (has-key? stringHash item)
            do((set stringHash item (+ 1 (get stringHash item )) ))

            (set stringHash item  1)
       )
       (console.log "adding" item)
    )

)
Jacob Rothstein
@jbr
Hi! what's the caller part?
currently caller is identical to arrayToHash
which doesn’t seem like what you want
powerserg19
@powerserg19
Sorry the caller part just some function could be foo.
I think I got it
I pass in a hash var e.g. by reference
Jacob Rothstein
@jbr
it’s maybe relevant that do((set stringHash item (+ 1 (get stringHash item )) )) won’t parse correctly
the do needs to be inside of parens. everything is s-expressions
powerserg19
@powerserg19
this is wha i have now
(def ath(arr sh)

    ;;(var stringHash (hash))
    (each item arr
       (if (has-key? sh item)        
            do((set sh item (+ 1 (get sh item )) ))                

            (set sh item  1)
       )
       (console.log "adding" item)
    )

)
and when i call it I can get the elements in sh
Jacob Rothstein
@jbr
the compiler interprets that as:
(def ath (arr sh)
    (each item arr
       (if (has-key? sh item) do
            (set sh item (+ 1 (get sh item )) )
            (set sh item  1))
       (console.log "adding" item)))
remove the do
or put it inside the parens, like (do (set sh item (+ 1 (get sh item))))
Jacob Rothstein
@jbr
and to golf that around a bit, this seems a bit more natural:
(def ath (arr sh)
    (each item arr
       (set sh item (+ 1 (if (has-key? sh item) (get sh item) 0)))))
powerserg19
@powerserg19
Thanks, Jacob. The do ended up in there based on uncertainty and Im still not clear what "do" does?
Jacob Rothstein
@jbr
if takes arguments of this shape: (if cond1 body1 cond2 body2… else)
so if you want multiple statements in the body of one of those conditions, you need a do
powerserg19
@powerserg19
oh. got it. thanks
Jacob Rothstein
@jbr
there are a few examples at https://docs.sibilant.org/if
in circumstances when there’s no else block, when is preferable, because (when condition (a) (b) (c)) is identical to (if condition (do (a) (b) (c)))
powerserg19
@powerserg19
Thank you again for going above and beyond!
Jacob Rothstein
@jbr
:heart: Thanks for learning sibilant!
Esther Suh
@estherjsuh
Hello, does sibilant support classes?
Aaron Beavers
@riatzukiza
No, but it can. I've writtten class macros before.
Jacob Rothstein
@jbr
first: // isn’t a comment. ; is a comment
Second: that do should be removed
while evaluates any number of expressions in the body https://docs.sibilant.org/while
third: based on the indentation, it seems like you intend to close the while after (get taskObject current), in which case you need an additional closing paren
Jacob Rothstein
@jbr
combined, without a close read of the code:
(def ordered-task (task task-object)
     (var ordered-tasks []) ;creates a new list
     (var current (first task)) ;tasks first element from task 
     (while (has-key? task-object current) ;while current is a key in taskObject
            (.unshift ordered-tasks (get task-object current)) ;get the object value, add to orderedTasks
            (assign current (get task-object current))) ;update current to last object value until current is not a key

     (each t task
           (.push ordered-tasks t)) ;add the  tasks to the end of the orderedTask array
     orderedTasks) ;return orderedTasks
that compiles to:
var orderedTask = (function orderedTask$(task, taskObject) {
  /* ordered-task sibilant.sibilant:1:0 */

  var orderedTasks = [];
  var current = task[0];
  (function() {
    var while$1 = undefined;
    while (taskObject.hasOwnProperty(current)) {
      while$1 = (function() {
        orderedTasks.unshift(taskObject[current]);
        return current = taskObject[current];
      }).call(this);
    };
    return while$1;
  }).call(this);
  task.forEach((function(t) {
    /* sibilant.sibilant:7:11 */

    return orderedTasks.push(t);
  }));
  return orderedTasks;
});
on a closer read, i think you want to assign to current
however, in general, this sort of imperative code doesn’t feel very natural in sibilant, and generally i’d look for a way to do this with something like a map or reduce
Esther Suh
@estherjsuh
@jbr thanks for helping!
I'd be happy to use map/reduce, is there a link to any docs for those methods?
Jacob Rothstein
@jbr
map and reduce are both the js array.prototype methods, so mdn has the docs
so for example (.map [1 2 3] (#(item) (+ 1 item)))
or even more concisely: (.map [1 2 3] (#-> (+ 1)))
Rasha Moumneh
@RashaMou
Hi all! I'm having what I think is a syntax error in my recursive function. When I run it I get a 'dft is not a function' error. Any thoughts welcome!
(def dft (map, subset, path, visited)
    (each item subset
        (if (excludes? visited item)
            (do
                (.push visited item)
                (dft(map (get map item) path done))
                (.push path item)
            )
        )
    )
    path
)