Skip to content

YS Curling (up North)

Ever been to Manitoba? I went there once when my flight from Seattle to Toronto diverted to Winnipeg because the plane's toilets stopped working! That's when I learned about the World's Largest Curling Rock External link .

Toronto DevOps Meetup

Speaking of Toronto, I'll be giving a talk called "The YS way to YAML" External link at the Toronto DevOps Meetup on June 12th. That's one week from today!

If you're in town I hope to see you there!

Today we'll be doing a little curling with YS.

Curling Web Data in YS🔗

Yesterday we learned how load data from other files.

Today we'll go a bit further on that and also learn how to load data from the web.

We have some nice shoes but we don't have anyone to wear them. We can fix that. I know of a big list of people who would love to try them on, and the list is a JSON array on the web External link !

Let's use YS to play around with that list.

$ ys -J -e '
url =:
  "https://github.com/dominictarr/random-name/raw/master/first-names.json"
json =: curl(url)
list =: json/load(json)
people =:
  take 3: list
say: count(list)
say: people
'
4946
(Aaren Aarika Abagael)

It looks like the "curling" function is called curl and it's built in. There's also a JSON loading built-in and a few other handy functions like take and count.

4946 is a lot of people! But why limit ourselves to the first 3? A random selection of 3 is a lot more fun!

$ ys -J -e '
url =:
  "https://github.com/dominictarr/random-name/raw/master/first-names.json"
json =: curl(url)
list =: json/load(json)
people =: list.shuffle().take(3)
say: people
'
(Johnna Korrie Elayne)

I like that better.

Did you notice the new function chaining syntax in list.shuffle().take(3)? We'll get into that a lot more later on, but let's have a little fun with it right now.

$ ys -J -e 'ARGS.0.curl().json/load().shuffle().take(3).say()' \
-- https://github.com/dominictarr/random-name/raw/master/first-names.json
(Madlen Joete Daveta)

That's pretty cool. We did everything in a single (chained) expression. Chaining functions together is one of the most powerful features of YS.

Put Your Shoes on!🔗

We have a list of people and a list of shoes. Let's pair them up!

$ ys -e '
url =:
  "https://github.com/dominictarr/random-name/raw/master/first-names.json"

people =: url.curl().json/load().shuffle().take(3)
shoes =: read("shoes.yaml").yaml/load()

say: str(people.0, " wears size ", shoes.0.size, " ", join([shoes.0.name, "s"]))
say: str(people.1, " wears size ", shoes.1.size, " ", join([shoes.1.name, "s"]))
say: str(people.2, " wears size ", shoes.2.size, " ", join([shoes.2.name, "s"]))
'
Shanon wears size 10 sneakers
Henrie wears size 10 boots
Sybil wears size 8 sandals

Looking good!

The shoes I mean. The code could be be way nicer. It will, but not today.

Notice that instead of using load on shoes.yaml we used read and yaml/load. TMTOWTDI strikes again!

Programming with YS🔗

So far we've been using YS to enhance YAML files. But can we use YS to write programs?

Well, we just did exactly that!

I wasn't expecting to show you that today, but that's just how it worked out. And we did all of it using YS -e one liners.

That last program is just begging to use loops and string formatting, right?

We'll get there, but for now let's get out in the sun and do some curling!

Comments