Skip to content

Instantly share code, notes, and snippets.

<!-- livebook:{"persist_outputs":true} -->
# zip create benchmark
```elixir
Mix.install([
{:benchee, "~> 1.0"}
])
```
defmodule Client do
def main(pid) do
cmd = IO.gets("Enter a command: ") |> String.trim()
case String.split(cmd) do
["show"] ->
key = self()
Task.async(fn ->
send(pid, {:list, key, self()})
main = fn ->
cmd = IO.gets("Enter a command: ") |> String.trim()
case String.split(cmd) do
["show"] ->
todos = Process.get(:todos) || []
IO.puts(Enum.join(todos, "\n"))
["create" | rest] ->
todos = Process.get(:todos) || []
main = fn ->
cmd = IO.gets("Enter a command: ") |> String.trim()
case cmd do
"1" ->
todos = Process.get(:todos) || []
IO.puts(Enum.join(todos, "\n"))
"2" ->
todos = Process.get(:todos) || []

Genetic Algorithm in Nx

Preface

After writing Genetic Algorithms in Elixir, I had no real hopes or expectations that numerical computing would become a focus for Elixir. However, to my surprise, the Nx project developed rather quickly, and proved that Elixir could be used for practical machine learning and numerical computing applications. While I don't have any plans (yet) of reworking the examples in my book to take advantage of the acceleration enabled by Nx, EXLA, and other projects, I feel it's necessary to show how you could go about rewriting your genetic algorithms to take advantage of Elixir's new numerical computing libraries.

Introduction

Nx is a numerical computing library for Elixir which supports the creation and manipulation of multi-dimensional arrays (called tensors in the API), automatic differentiation, and just-in-time (JIT) compilation to CPU, GPU, and other accelerators via pluggable backends and compilers. Nx opens up a realm of possibilities for Elixir dev

@seanmor5
seanmor5 / wine_documents.jsonl
Created September 27, 2022 23:37
Wine Documents
This file has been truncated, but you can view the full file.
{"name": "Chateau de Landiras 2018", "url": "https://www.wine.com//product/chateau-de-landiras-2018/677595", "varietal": "Bordeaux Red Blends", "location": "Graves, Bordeaux, France", "alcohol_volume": "750ML", "alcohol_percent": "0", "price": "$19", "notes": "Blend: 75% Merlot, 25% Cabernet Sauvignon", "reviews": [{"author": "Decanter", "review": "Grand generosity, showcasing deep, vigorous dark forest fruit and an appetising blackcurrant freshness within juicy and fabulously-integrated tannins. Lovely, boisterous and rich in fruit. Bravo!", "rating": "95"}]}
{"name": "Silver Oak Napa Valley Cabernet Sauvignon 2016", "url": "https://www.wine.com//product/silver-oak-napa-valley-cabernet-sauvignon-2016/683756", "varietal": "Cabernet Sauvignon", "location": "Napa Valley, California", "alcohol_volume": "750ML", "alcohol_percent": "14.2", "price": "$144", "notes": "Deep ruby in color, the 2016 Napa Valley Cabernet Sauvignon is an opulent wine with notes of cassis, ripe raspberry, toasted coconut and shiitake mush
defmodule Day3 do
# Since my last answer wasn't purely Nx, I'm going
# to try to stick to Nx as much as is possible, but
# we don't have string manipulation stuff so that will
# have to be done in Elixir
import Nx.Defn
def part1 do
File.read!("aoc/3.txt")
|> parse_input()
defmodule Day4 do
import Nx.Defn
def part1() do
File.read!("aoc/4.txt")
|> parse_input()
|> play_bingo()
|> find_winning_board()
end
# part 1
File.read!('aoc/2.txt')
|> String.replace("\r", "") # i am on windows right now :(
|> String.split("\n")
|> Enum.map(fn line ->
[direction, amt] = String.split(line)
amt_int = String.to_integer(amt)
case direction do
"forward" ->
# Part 1
File.read!('aoc/1.txt')
|> String.replace("\r", "") # i am on windows right now :(
|> String.split("\n")
|> Enum.map(&String.to_integer/1)
|> Nx.tensor()
|> then(fn x ->
Nx.greater(Nx.slice_axis(x, 1, Nx.size(x) - 1, 0), Nx.slice_axis(x, 0, Nx.size(x) - 1, 0))
end)
|> Nx.sum()