Skip to content

2025🔗

YS One Liners

There's almost nothing I like more about programming than one liners.

A one liner is a single line of code that does something useful and doesn't require any extra steps to compile or run.

You type one line, press enter, and get your result.

I first learned about one liners in Perl.

If we have a file.txt with the following content:

one
two
three
four
five

Here's a Perl one liner that counts the number of lines in a file:

$ perl -E '@l = <>; say scalar(@l)' < file.txt
5

The gist of YS

Yesterday we started learning about the ys CLI and there's a lot more cool stuff to learn about it.

But today I want to switch it up and talk about one of my favorite programs that I use many times a day.

You probably know about GitHub gists. They are one of the best ways to share text files with others.

Let's take a look!

The ys Command

There are different ways to use YS but the most common is to use it via the YS command-line tool: ys; a very versatile tool indeed.

There's a lot you can do with ys including using it like you would use jq or yq one-liners:

$ jq .bar < <(echo '{"foo": 123, "bar": 456, "baz": 789}')
456
$ yq .bar < <(echo -e 'foo: 123\nbar: 456\nbaz: 789')
456
$ ys .bar < <(echo -e 'foo: 123\nbar: 456\nbaz: 789')
456

Each of these tools have their own advantages and we'll be diving deep into those waters soon enough.

Today let's just start by exploring the basic things you can do with the ys CLI.

Fancier YS Conditionals

Most languages support a case or switch construct which is a way to handle multiple conditions.

YS supports several similar but different constructs for this.

Today we'll focus on the cond and case functions.

When to when

Yesterday we mentioned the when function as an alternative to if.

It might seem like a weaker form, so why would you use it?

It turns out that when is is really useful for a few reasons. You might end up using it more than if!

if - We Must

Yesterday we talked about if being a special form.

It turns out that if is special in other ways. For instance, it requires both the then and else clauses to be present… but… it doesn't require the actual then and else keywords.

Today we'll finish our conversation about if and go over all the specifics.

if - You Are Special

Yesterday I told you that if is a function.

I lied.

In reality, if is a "special form".

I'll explain special forms in a minute, but let's just say that they're "special"!

YS Multi Functions

Many languages let you define functions with multiple signatures.

That is, you can have multiple functions with the same name but different arguments.

YS supports multi-arity functions. It dispatches on the number of arguments.

Let's see how it works.

Where the Funcs Have No Name

Any self-respecting functional language has a way to create anonymous functions.

In YS there are more than one!

Today we'll talk about nameless functions, why they are useful, and how to create and call them.

Functionally Speaking

YS is a functional programming language.

Therefore we should be able to write functions in YS.

This week I want to do a mini-series of blog posts about using and writing functions in YS.

Let's get started!

In The Beginning

How did YAML get started in the first place?

Today's Sunday post is about YAML, not YS.

But since YS is YAML, I think that's OK.

This is the story of how the YAML data language got its start as far back as 1999.

React and Comment here please!!!

I'm not sure how many people are reading this series, but I'm enjoying writing it. It's nice to know that I'm not alone on this Summer journey.

I recently added Reactions and Comments support to these blog posts. Scroll down to the bottom of each post to see them.

If you are enjoying the Summer of YS series, please don't forget to react (and comment if you want to) to let me know!

TMTOWTDI for YS Expressions

Like I said before, in YS, There's More Than One Way To Do It.

This is especially true for YS expressions.

Later on you'll learn that YS is a Lisp in disguise. In Lisp, an expression is a list consisting of a function and its arguments inside a set of parentheses.

Consider this Python code:

name = "World"
print("Hello, " + name + "!")

In a Lisp, this would be written as:

(def name "World")
(println (str "Hello, " name "!"))

In YS, this could be written as:

name =: 'World'
say: str('Hello, ' name '!')

I say could because... TMTOWTDI!

Today I'm going to show you many of the ways to DO IT in YS.

Loops and Strings

Yesterday I left you with a program that really needed to "YS 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"]))
'

Let's make this awesome!

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.

Toronto DevOps Meetup

Speaking of Toronto, I'll be giving a talk called "The YS way to YAML" 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.

Load and Compose your YAML Files

Probably the biggest problem people have with YAML is that everything has to be in one file. Things start off nice and clean, but as requirements grow, so do your files!

What if you could compose your YAML documents like you compose your code? Lots of small, single-purpose, possibly reusable files that you can load and compose together into the thing you need?

That's what YS is all about. As you know, YS is a functional language, and it has quite a few ways to load data (and code too, since Code is Data™!) from external sources.

Today we'll be looking at how to load things from disk files, including:

  • Other YS files
  • YAML files (YAML is YS)
  • JSON files (JSON is YAML)

You can also load things from CSV/TSV files, shell commands, databases, APIs, environment variables, and the web, but those are topics for another day.

YS YAML Documents

YAML files (aka YAML streams) can contain multiple "documents".

A YAML document is a top level mapping or sequence "node". Most YAML files contain a single document, but YAML files can contain multiple (or zero!) documents. New documents are started with a line of three dashes: ---.

YS can put these documents to all kinds of good use.

When you "load" a YAML file with YS, the result is the evaluation of the final document (by default). But since YS is functional, it can access any of the other documents.

Let's continue with yesterday's shoes example.

YAML Variables

YAML itself isn't a functional programming language, but advanced users are probably aware of YAML's anchors, aliases and the merge key.

The merge key is YAML's one functional thing, and it's actually not even part of the YAML 1.2 spec. However, people find it useful and many YAML implementations (including YS) support it.

The merge key (<<) is a special key that allows you to merge the contents of one mapping into another.

Today we'll explore the merge key a bit and show how variables can make it nicer to use.

The Summer of YS

Today starts a 3 month long, daily summertime journey into the intricacies of YAML and the wisdom of YS! Put on your favorite pair of coding sunglasses, grab a refreshing config drink, and let's get started!

Back in March I promised to start writing more often about all the ways that YS can help you out day-to-day with your YAML interactions. When I last posted here it was barely Spring and now Spring is turning into Summer.

Seasonal turning points be damned, I think of Summer as June, July, and August. In other words...

It's Summer dammit!

Let's declare this Summer, The Summer of YS!

YAMLScript is YS!

Greetings! And welcome back to YAMLScript in 2025!

Or as we now say, YS in '25!.

It's been a minute since our last update, but we've been working super hard to make YS the best it can be.

Oh… What's "YS", you say?

Well, don't say "Y-S"…

Say "Wise"!