Skip to content

The YS Blog🔗

YS / Java Interoperability

We know that YS is made from Clojure and the Clojure is made from Java. Clojure code is interoperable with Java code.

So is YS interoperable with Java?

Yes, YS is can call Java methods on its objects.

Let's see how to do it.

YS Shorties

From the start, YAML has always been about making data clean and easy to read.

This carries over to YS code as well.

Even though YS code compiles to Clojure, YS often has shorter alternatives for Clojure's commonly used and longer function names.

Fun FridaYS — Rosetta Code

It's Friday and I feel like having some fun. With YS, of course.

Rosetta Code is a super fun site that has over 1000 programming tasks that people solve in nearly 1000 programming languages (including YS). If you've never heard of it, you should check it out.

Let's solve a task in YS that hasn't been solved yet!

YS Mode Switching

I've mentioned YS "modes" in passing several times in this series.

YS has 3 modes: data, code, and bare.

Fully understanding modes is one of the most important things to understand about YS.

Today I want to go deeper on the details of modes. This will make everything else much easier to explain going forward.

AI + Clojure Functions in YAML

Yesterday we learned that all YS YAML input compiles to Clojure (Lisp) before being evaluated by a native binary Clojure interpreter runtime.

Does this mean that you could write Lisp functions in your YAML data files? And then call them on your data?

Of course it does!

How Does YS Work?

What if I told you that...

  • YS is made out of Java
  • YS uses no JVM
  • YS is a binary Executable
  • YS is also a Shared Library
  • YS is actually a Lisp
  • YS can use S-Expressions
  • YS prefers YeS-Expressions
  • YS can use modules written in:
    • YS
    • Any other language

Make sense?

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.