This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Parens do | |
def generate(n) do | |
n | |
|> generate_trees() | |
|> Enum.map(&IO.iodata_to_binary/1) | |
end | |
defp generate_trees(0), do: [] | |
defp generate_trees(1), do: [["()"]] | |
defp generate_trees(n) do |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Primality do | |
# O jeito "ingênuo" de checar por primalidade é você simplesmente verificar | |
# se o número tem divisores. Ou seja, você pega todos os números menores | |
# que o seu número-alvo (começando em 2, pois 1 divide todos os números) | |
# e verifica se eles dividem o número (ou seja, se o resto da divisão - | |
# função `rem/1` - é igual a 0). | |
# | |
# Também precisamos de um caso especial para 1, que não tem divisores mas não | |
# é primo. | |
def check_naive(1), do: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Change do | |
# A intuição para resolver este problema é entender que | |
# quando você vai fazer troco para um valor, dado um certo | |
# conjunto de moedas, você tem duas escolhas: | |
# | |
# 1. Você pode usar a moeda de maior valor que você tem | |
# 2. Você pode fazer troco sem usar essa moeda. | |
# | |
# Se você escolher a opção 1, você está implicitamente dizendo | |
# que você vai usar pelo menos 1 moeda daquele tipo. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule BalancedDelimiters do | |
@openers [ | |
?(, | |
?[, | |
?{ | |
] | |
@closers [ | |
?), | |
?], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Notar que Haskell não usa parênteses pra chamar funções, então `f a b` seria `f(a, b)` em | |
-- outras linguagens | |
-- suponhamos um joguinho estilo space invaders | |
-- aí em cada frame você verifica se uma bala tá colidindo com algum inimigo | |
-- e se tiver colisões isso causa atualizações específicas do estado | |
evalFrame gameState = | |
case getBulletCollisions gameState of | |
[] -> continueEvalFrame gameState -- lista vazia, nenhuma colisão, só executamos outros updates | |
collisions -> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Imagina que a gente não tivesse recursão. Isso significaria essencialmente que uma expressão não pode se referir ao nome | |
-- dela mesma dentro da definição dela. Por exemplo, a definição de fatorial abaixo não poderia existir: | |
factorialRec n = n * factorialRec (n - 1) | |
-- Como o nome factorialRec é citado na função e estamos proibindo citar o nome da própria função, esse termo se torna | |
-- ilegal. Então como poderíamos escrever a função fatorial? Bom, uma ideia é trocar a chamada recursiva por um argumento: | |
factorialOpen f n = n * f (n - 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const makeRuleSet = (rules) => | |
new Map( | |
rules | |
.flat() | |
.map(([lprop, rprop, effects]) => [[lprop, rprop], effects]) | |
); | |
const simpleRule = (rule) => [rule]; | |
const commutativeRule = (rule) => [rule, reverseRule(rule)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule DeepMerge do | |
def deep_merge(m1, m2) do | |
Map.new(m1, fn {k, v1} -> | |
case Map.get(m2, k) do | |
nil -> {k, v1} | |
v2 -> {k, deep_merge_values(v1, v2)} | |
end | |
end) | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch('firstUrl') | |
.then((firstResponse) => fetch('secondUrl', { method: 'POST', body: mkSecondReqBody(firstResponse) })) | |
.then((secondResponse) => fetch('thirdUrl', { method: 'POST', body: mkThirdReqBody(secondResponse) })) | |
.catch((error) => handleError(error)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Promise.all([ | |
fetch('firstUrl'), | |
fetch('secondUrl'), | |
fetch('thirdUrl') | |
]).then(([firstResponse, secondResponse, thirdResponse] => { | |
// faz o que quiser aqui | |
}).catch((error) => { /* trata erro aqui */ }); |
NewerOlder