Skip to content

YS Operators

YS has a number of operators that you can use in your code.

Review YeS Expressions to see how YS supports infix operator expressions, whereas Clojure requires the operator to precede the operands.

When YS operators are used with infix notation, they often become polymorphic and do things according to the types of the operands.

!ys-0
s =: ('foo' + 'bar')  # => 'foobar'
s =: (+ 'foo' 'bar')  # ERROR - Clojure + only works on numbers

You can see why by looking at the ys -c output:

$ ys -c -e "s =: ('foo' + 'bar')" -e "s =: (+ 'foo' 'bar')"
(def s (add+ "foo" "bar"))
(def s (+ "foo" "bar"))

See how the infix + operator compiles to the add+ function? The add+ function works on numbers, strings, characters, sequences, mappings, sets and functions!

Note

If you absolutely need the Clojure + function for performance reasons, you can simply use the prefix form: (+ a b).

Arithmetic Operators🔗

  • + - Addition - Infix works on strings, sequences, and mappings or else casts arguments to numbers.
  • - - Subtraction - Infix also removes matching text or collection members.
  • * - Multiplication - Infix works on numbers or str * num, num * str, seq * num, num * seq.
  • / - Division - Works on numbers. Infix returns a double in the cases where Clojure would return a ratio.
  • // - Quotient - Works on numbers. Compiles to quot in Clojure.
  • % - Remainder - Works on numbers. Compiles to rem in Clojure.
  • %% - Modulus - Works on numbers. Compiles to mod in Clojure.
  • ** - Exponentiation - Works on numbers and has right associativity.

Set Operations🔗

Sets use \{...} syntax; \(...) creates an anonymous function.

Operator Set operation
+ Union
- Difference
* Intersection
<, <= Proper subset, subset
>, >= Proper superset, superset

Arithmetic accepts two sets, or a set and a vector or sequence in either order. The result has the left operand's collection family: set, vector, or sequence. Vector and sequence results preserve left-side order and duplicates. Addition appends the right collection's elements, whose order is unspecified when the right operand is a set.

!ys-0
say: \{1 2 3} + [3 4 5]    # #{1 2 3 4 5}, in unspecified order
say: ([3 1 3 2] - \{2})    # [3 1 3]
say: ([3 1 3 2] * \{3})    # [3 3]
say: \{1} < \{1 2}        # true
say: \{1} < \{1}          # false
say: \{1} <= \{1}         # true

Subset and superset comparisons require two sets. They test membership, rather than comparing collection sizes. Comparisons can be chained, such as \{} < \{1} < \{1 2}. The named functions lt, le, gt, and ge support the same behavior.

Subtracting a scalar from a set still removes that single member. Subtracting a collection removes its members. To remove a collection-valued member, wrap it in a set: \{[1 2] 3} - \{[1 2]} produces \{3}. Operations do not recursively flatten nested collections.

Comparison Operators🔗

  • == - Equal To - Works on any comparable values.
  • != - Not Equal To - Works on any comparable values.
  • > - Greater Than - Works on numbers and sets. Supports a > b > c.
  • >= - Greater Than or Equal To - Works on numbers and sets.
  • < - Less Than - Works on numbers and sets.
  • <= - Less Than or Equal To - Works on numbers and sets.

These operators have the respective named functions: eq, ne, gt, ge, lt, le for use in places where a function makes more sense than an operator.

Conditional Operators🔗

In Clojure false and nil are treated as "false" and everything else is treated as "true".

YS adds the concept of "truey" and "falsey" values. Empty strings, empty collections, 0, false, and nil are "falsey" and everything else is "truey".

This concept applies to some operators.

  • && - Logical And
  • || - Logical Or
  • &&& - Truey And
  • ||| - Truey Or

Regular Expression Operators🔗

  • =~ - a =~ b calls re-find(b a). Coerces a to a string.
  • !~ - not(a =~ b)
  • =~~ - a =~ b calls re-matches(b a). Coerces a to a string.
  • !~~ - not(a =~~ b)

Other Operators🔗

  • . - Function Chaining Operator

    a.b.3.c(d).e(f) compiles to:
    (e (c (nth (get+ a 'b) 3) d) f).

  • .. - Range Operator

    1 .. 3 -> (1 2 3), 3 .. 1 -> (3 2 1).

    Differs from Clojure's range External link function.