Skip to content

Instantly share code, notes, and snippets.

@dantswain
dantswain / dts_k_means.livemd
Created November 28, 2021 03:23
K-Means Clustering with Elixir NX

K-Means Clustering

The data

Mix.install([
  {:nx, "~> 0.1.0-dev", github: "elixir-nx/nx", sparse: "nx", override: true},
  {:vega_lite, "~> 0.1"},
  {:kino, "~> 0.3"}
])
warning: the VM is running with native name encoding of latin1 which may cause Elixir to malfunction as it expects utf8. Please ensure your locale is set to UTF-8 (which can be verified by running "locale" in your shell)
warning: the VM is running with native name encoding of latin1 which may cause Elixir to malfunction as it expects utf8. Please ensure your locale is set to UTF-8 (which can be verified by running "locale" in your shell)
ok
vm.args needs to have either -name or -sname parameter.
+ uname
+ grep -q Darwin
++ readlink -f /usr/local/ourco/ourapp-1.1.3/releases/1.1.3/ourapp.sh
+ SCRIPT=/usr/local/ourco/ourapp-1.1.3/releases/1.1.3/ourapp.sh
++ dirname /usr/local/ourco/ourapp-1.1.3/releases/1.1.3/ourapp.sh
+ SCRIPT_DIR=/usr/local/ourco/ourapp-1.1.3/releases/1.1.3

Keybase proof

I hereby claim:

  • I am dantswain on github.
  • I am dantswain (https://keybase.io/dantswain) on keybase.
  • I have a public key whose fingerprint is 6C6B A60E D74A C07B 6124 F3DA A171 B2F0 C78D 87D4

To claim this, I am signing this object:

@dantswain
dantswain / thrash_test.thrift
Created October 16, 2016 17:22
Example thrift file with enum and typedef
namespace erl thrash
typedef i64 UgeInt
const i32 MAX_THINGS = 42
enum TacoType {
BARBACOA = 123,
CARNITAS = 124,
STEAK = 125,
@dantswain
dantswain / gist:d517a57eac7cd19f2c07
Created February 18, 2016 19:57
KafkaEx consumer group begin set to "kafka_ex"
iex(1)> KafkaEx.create_worker(:foo, uris: Application.get_env(:kafka_ex, :brokers), consumer_group: "bar")
14:53:54.712 [debug] Succesfully connected to "localhost" on port 9092
14:53:54.714 [debug] Succesfully connected to "localhost" on port 9093
14:53:54.716 [debug] Succesfully connected to "localhost" on port 9094
{:ok, #PID<0.138.0>}
iex(2)> Enum.each(1..1000, fn x -> KafkaEx.produce("foo", 0, "bar#{x}") end)
:ok
@dantswain
dantswain / gist:fdfb1c2c86e4d940a8f5
Last active May 3, 2020 04:05
Convert Elixir config.exs to Erlang sys.config
#!/usr/bin/env elixir
# Convert an Elixir config.exs to an erlang sys.config
#
# Usage: elixir_to_sys_config config.exs > sys.config
# First argument is the Elixir config.exs file
# Writes to stdout
# You probably want to set MIX_ENV accordingly
#
# 2015 by Dan Swain, dan.t.swain@gmail.com
@dantswain
dantswain / gist:c1198e27f95664449fec
Created April 7, 2015 15:30
Ruby GC and finalizers
# encoding: utf-8
class Bar
def initialize(name)
@name = name
end
def hi
puts "HI #{@name}"
end
@dantswain
dantswain / gist:c3709e249a174d42e59a
Last active August 29, 2015 14:17
Javascript Shaming

To produce a string a la '2015-03-25' for the current date

In Ruby:

require 'date'
d = Date.today
puts d.to_s

In Javascript:

@dantswain
dantswain / ErlangFirstMatch
Created September 30, 2012 15:17
Erlang first match functions (doesn't evaluate past first match)
% From http://stackoverflow.com/questions/12657202/
-export([first/3, first1/3]).
% My solution
first([E | Rest], Condition, Default) ->
case Condition(E) of
true -> E;
false -> first(Rest, Condition, Default)
end;
first([], _Cond, Default) -> Default.
@dantswain
dantswain / gist:1981581
Created March 5, 2012 22:22
how to get a stdout/stderr console in windows
if(AllocConsole())
{
freopen("CONOUT$", "wt", stdout);
freopen("CONOUT$", "wt", stderr);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
fprintf(stdout, "\n");
fprintf(stdout, " stdout/stderr Console\n");