re: What are your favorite programming language syntax features? VIEW POST


Most languages have something nice, but here are a few random things I like:

  • Left/right sections in Haskell: (2+) This is essentially a function that adds 2 to its — anonymous — argument:
map (2+) [1..10]
=> [3,4,5,6,7,8,9,10,11,12]
  • Threading/pipe macros and operators like Clojure's ->, ->> and |> in Elixir and F# since they allow to rewrite expressions in terms of data flow instead of function application order.
iex> "Elixir rocks" |> String.upcase() |> String.split()
  • Erlang's bit syntax, which allows pattern matching on binary data, like the following pattern that describes the parts of an MP3's id3 tag and extracts it into relevant variables (see in use here):
  • CommonLisp's format macro, even though it always scared me a bit. There are even format strings for converting numbers to ordinals or Roman numerals:
(format nil "~:r" 1234) ==> "one thousand two hundred thirty-fourth"
(format nil "~@r" 1234) ==> "MCCXXXIV"
  • Not really syntax, but MicroPython's asm_thumb annotation is super handy for writing parts of your application in ARM assembly.
def asm_add(r0, r1):
    add(r0, r0, r1)

print(asm_add(1, 2))

All these things almost look alien to me. I haven't done any heavy functional programming and let alone use a language that was specifically built for such purpose. This is something I'll definitely look into.

Code of Conduct Report abuse