Fork me on GitHub
#yamlscript
<
2024-05-16
>
Daniel Craig02:05:08

I did this matrix multiplication toy implementation based on some code that @sritchie09 put on stack overflow years ago, and it’s got me thinking that yamlscript is just as approachable as Python https://github.com/danielmartincraig/yamlscript-experiments/blob/master/matrix.ys

Ingy döt Net22:05:43

Very nice:

$ curl -s  | time ys -
((3 2 1) (6 5 4) (9 8 7))
0.00user 0.03system 0:00.08elapsed 42%CPU (0avgtext+0avgdata 48000maxresident)k
0inputs+0outputs (0major+4746minor)pagefaults 0swaps
🙂

Ingy döt Net22:05:15

Sorry I missed this earlier.

Ingy döt Net22:05:35

How do I get all messages in #C05HQFMTURF to notify me?

Ingy döt Net22:05:00

I just get notifications for mentions and threads I'm active in...

Sam Ritchie20:05:21

click on the channel name to get settings, then change the dropdown here

Daniel Craig02:05:59

What’s the syntax for an anonymous function in yamlscript? Apologies if you already explained to me

Ingy döt Net22:05:33

$ ys -e 'foo =: \(%1 * %2)' -e 'foo: 6 7' -c
(def foo (fn [& [_1 _2]] (*_ _1 _2)))
(foo 6 7)

$ ys -e 'foo =: \(%1 * %2)' -e 'foo: 6 7' -p
42

Ingy döt Net22:05:08

Note that you can't use %, you need to use %1

Ingy döt Net22:05:23

% is the rem operator in YS:

$ ys -e 'say: 42 % 13'
3

Ingy döt Net22:05:54

(also %% is the mod operator)

Ingy döt Net22:05:57

Of course you can always use fn (as in clojure)

$ ys -e 'foo =: (fn [a b] (a * b))' -e 'foo: 6 7' -p
42

Ingy döt Net22:05:41

with less parens:

$ cat x.ys 
!yamlscript/v0
foo =:
  fn [a b]: a * b
say:
  foo: 6 7
$ ys x.ys 
42

Daniel Craig23:05:02

Awesome thank you!!

1
Daniel Craig00:05:11

This is great help

Ingy döt Net00:05:48

I'll review your matrix code more closely now

Ingy döt Net00:05:04

It looked pretty good when I glanced at it

Ingy döt Net00:05:13

I did a bunch of things different to show off various ways to do things

Ingy döt Net00:05:40

My way is not the correct way, just an alternate way

Ingy döt Net00:05:26

The one thing that YS won't do for you is offer a single syntax, like a Lisp does. I know some people like this because it keeps that part of the language simple. YS lets you do stuff in lots of ways. Some are close to Lisp and some avoid all parens, and some are in between. But if you ok with that, YS can offer you some really cool syntactic things.

Ingy döt Net00:05:44

One thing I struggle with constantly is remembering argument order for things like take and nth and pretty much anything that takes multiple args

Daniel Craig00:05:01

This is great, I’m intrigued by moving the : to the places where you moved it

Ingy döt Net00:05:38

get-some-seq().take(5) used to blow up because the seq gets inserted first. One solution is get-some-seq().take(5 _) which works but angered me.

Daniel Craig00:05:53

apply map vector: m is different than I was expecting, I think I thought the : always had to follow the first thing in the expression

Ingy döt Net00:05:55

So I made I made:

$ ys -ce 'get-some-seq().take(5)'
(__ (get-some-seq) (list +take 5))

Ingy döt Net00:05:23

and +take checks it's args and DTRT. slower but cleaner and no need to remember. If speed counts then:

$ ys -ce 'get-some-seq().take(5 _)'
(__ (get-some-seq) (list take 5 '_))
is still available

Ingy döt Net00:05:39

$ ys -ce '
a: b c d
a b: c d
a b c: d
a b c d:'
(a b c d)
(a b c d)
(a b c d)
(a b c d)
😄

Ingy döt Net00:05:25

if:
  (a > b)
  bar
  baz
works fine, but I'd rather see:
if (a > b):
  bar
  baz

Ingy döt Net00:05:28

but you can

if (a > b) bar: baz
if that's your thing 🙂

Ingy döt Net00:05:05

We don't support:

$ ys -ce ': a b c d'
Error: while parsing a block mapping
 in reader, line 2, column 1:
    : a b c d
    ^
That's SnakeYAML-Engine's fault not mine. It's actually valid YAML according to the 1.2 spec: https://play.yaml.io/main/parser?input=OiBhIGIgYyBk

Ingy döt Net00:05:27

But we are talking about making it invalid

Daniel Craig00:05:52

Haha oh my gosh so the : can be anywhere that makes sense to the author almost

1
Ingy döt Net00:05:34

That's spot on

Ingy döt Net00:05:08

That reminds me, today I got a replacement for SnakeYAML working. RapidYAML (world's fastest known YAML parser in C++)

Ingy döt Net00:05:54

Integrating C++ into Clojure isn't for the faint of heart 😄

1
Ingy döt Net00:05:38

in the next release you can enable it with export YS_PARSER=rapidyaml

Ingy döt Net00:05:24

It comes as a shared library with a JNA wrapper. Still working on the cleanest install strategy.