Exercism Comparisons
Last week I told you about YS on Rosetta Code and yesterday I told you about YS on Exercism.
Today I'm going to show you how to compare solutions to the same Rosetta Code task using all the Exercism languages!
Last week I told you about YS on Rosetta Code and yesterday I told you about YS on Exercism.
Today I'm going to show you how to compare solutions to the same Rosetta Code task using all the Exercism languages!
Ever heard of a YS Exorcism?!
Sounds pretty scary!
A YS Exercism on the other hand is one of the best ways to learn YS.
Exercism.org is a free programming learning site with tracks for over 75 languages!
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.
Today I'd like to show you why I love how YS handles symbols.
Symbols are used to represent variables, functions, and other names in the language.
All the shorties I showed you yesterday were symbols. Symbols that resolved to functions.
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.
Or better yet...
If I were to tell you that YS has a system,
to determine if something is true...
I'd deserve hell and fire,
and be labeled a liar,
for in total truth, YS has 2!
Yesterday we wrote a YS program by porting a Clojure program from Rosetta Code.
We introduced a bunch of new things without really explaining them.
Today I'll explain a set of them.
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!
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.
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!
What if I told you that...
Make sense?
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
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!
ys
CommandThere 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.
Do you know how a YS assignment actually works?
It compiles into a Lisp let
form.
Let's take a look...
Yesterday we looked at the cond
and case
functions.
The cond
function has a couple cousins: condp
and condf
.
Let's take a look at them.
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.
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
!
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.
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"!
Today we're going to look at the if
command.
In a functional language, if
statements are a bit different.
They'll, well, um... Functions.
So what's the big deal?
Last night a gave a talk about YS and YAML to the Toronto DevOps Meetup.
I had a lot of fun, and met a lot of great people.
Today I'm going to share one of the things I covered last night. Global variables.
It's not the most exciting topic, but it can be useful.
Tonight I'm giving a talk about YS and YAML to the Toronto DevOps Meetup.
What could be more important to a developer than environment variables?!
Let me show you how to use environment variables in YS and in your YAML files.
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.
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.