Code Snippets Erlang

Short texts Erlang 🧑‍💻 Code snippets Erlang ✍️ Reading lists Erlang 👀

Double Cola problem in Erlang

%% Double Cola problem in Erlang %% https://codeforces.com/problemset/problem/82/A -module(main). -export([start/1]). start(N) -> io:fwrite( cola(N-1, [ {“Sheldon”, 1}, {“Leonard”, 1}, {“Penny”, 1}, {“Rajesh”, 1}, {“Howard”, 1}]) ++ “\n”). cola(N, [{Name, Count} | TheRest]) when N-Count >= 0 -> cola(N-Count, TheRest ++ [{Name, Count * 2}]); cola(N, [{Name, Count} | _]) when N-Count < 0 -> Name.

Separate ssl options

%% Copyright (c) 2011-2018, Loïc Hoguin %% %% Permission to use, copy, modify, and/or distribute this software for any %% purpose with or without fee is hereby granted, provided that the above %% copyright notice and this permission notice appear in all copies. %% %% THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS […]

Join a list of elements in Erlang, adding a delimiter between them

%% @doc Join a list of elements adding a delimiter between %% each of them. -spec join(iolist(), Sep :: term()) -> iolist(). join([], _Sep) -> []; join([Head | Tail], Sep) -> join_list_sep(Tail, Sep, [Head]). -spec join_list_sep(iolist(), Sep :: term(), Acc :: iolist()) -> iolist(). join_list_sep([], _Sep, Acc) -> lists:reverse(Acc); join_list_sep([Head | Tail], Sep, Acc) -> […]

Using verl to get your semver app’s .

version() -> [{_AppNameStr, Ver, _LibVers, permanent}] = release_handler:which_releases(permanent), {ok, Version} = verl:parse(list_to_binary(Ver)), Major = integer_to_list(maps:get(major, Version)), Minor = integer_to_list(maps:get(minor, Version)), Major ++ “.” ++ Minor.

A polyglot summation challenge

The summation challenge, extended from the classical a-plus-b problem, is a programming challenge for beginners. A programmer is asked to write a program to read multiple integers separated by whitespace from a single line and then print out their sum. Although not difficult, different solutions to this problem can exhibit different characteristics of programming languages. […]