Skip to content

Instantly share code, notes, and snippets.

@vKxni
vKxni / discord-servers
Last active April 9, 2023 10:41
Collection of Discord servers related to programming languages
Assembly: https://discord.com/invite/Km5VzKj
Elixir: https://discord.gg/NzSUHekD3t
Ballerina: https://discord.com/invite/wAJYFbMrG2
Beef: https://discord.gg/rnsc9YP
Backlang: https://discord.com/invite/36SsV3vy4p
Caramel: https://discord.com/invite/yvCcjNmCcY
Idris: https://discord.com/invite/YXmWC5yKYM
R: https://discord.gg/GfAzp8umH8
Reason: https://discord.com/invite/reasonml
Ruby: https://discord.gg/mk7mBwadub
@vKxni
vKxni / binary.ex
Created June 6, 2022 09:54
Eixir Binary
defmodule Binary do
def write(text, filename) do
binary = :erlang.term_to_binary(text)
File.write(filename, binary)
end
def read(filename) do
case File.read(filename) do
{:ok, binary} -> :erlang.binary_to_term(binary)
{:error, _reason} -> "The file does not exist."
@vKxni
vKxni / todoList.ex
Created July 5, 2022 11:40 — forked from hassanRsiddiqi/todoList.ex
TodoList in Elixir using GenServer.
defmodule TodoList do
use GenServer
@moduledoc """
A TodoList add, list, find, update & remove.
"""
def start do
{:ok, state} = GenServer.start_link(__MODULE__, [])
state
end
defmodule Main do
def print, do: "Hello from Main"
end
defmodule Other do
defdelegate hello, to: Main, as: :print
end
IO.puts(Other.hello)
@vKxni
vKxni / format.ex
Created July 30, 2022 09:06
Format big numbers with Elixir
defmodule Number do
def format(input, separator \\ ",") do
prefix_size = rem(byte_size(input), 3)
{prefix, rest} = String.split_at(input, prefix_size)
segments = for <<chunk::binary-size(3) <- rest>>, do: chunk
Enum.join([prefix | segments], separator)
end
end
@vKxni
vKxni / word_count.ex
Created September 25, 2022 19:47
Count the number of words in the sentence
defmodule Utils.WordCount do
@doc """
Count the number of words in the sentence.
Words are compared case-insensitively.
"hello there"
hello: 5
friends: 7
"""
@spec count(String.t()) :: map
@vKxni
vKxni / space_age.ex
Created September 25, 2022 19:47
Returns the number of years a person that has lived for 'seconds' seconds is aged on 'planet'
defmodule Utils.SpaceAge do
@type planet ::
:mercury
| :venus
| :earth
| :mars
| :jupiter
| :saturn
| :uranus
| :neptune
defmodule BirdCount do
def today([]), do: nil
def today(list), do: hd(list)
def increment_day_count([]), do: [1]
def increment_day_count([h | t]), do: [h + 1 | t]
def has_day_without_birds?([]), do: false
def has_day_without_birds?([0 | _]), do: true
def has_day_without_birds?([_ | t ]), do: has_day_without_birds?(t)
@vKxni
vKxni / heart.ex
Created September 25, 2022 19:49
Hearten with the Kernel
defmodule Heart do
def first_letter(name) do
name
|> String.trim()
|> String.first()
end
def initial(name) do
name
|> first_letter()
@vKxni
vKxni / kitchen_calculator.ex
Created September 25, 2022 19:50
Calculate stuff for Kitchen
defmodule KitchenCalculator do
def get_volume(volume_pair) do
elem(volume_pair, 1)
end
def to_milliliter({:milliliter, volume}), do: {:milliliter, volume}
def to_milliliter({:cup, volume}), do: {:milliliter, volume * 240}
def to_milliliter({:fluid_ounce, volume}), do: {:milliliter, volume * 30}
def to_milliliter({:teaspoon, volume}), do: {:milliliter, volume * 5}