Skip to content

Instantly share code, notes, and snippets.

@safiire
safiire / rc_filter_simulation.scm
Last active September 4, 2015 02:51
RC Filter Circuit simulation in Scheme
;;;; RC Filter Simulation
;; Calculates the frequency response of an RC filter
;; and displays it with ASCII art
;; This is just a program I always write to learn a new programming language
;; I tried out Scheme's lazy lists for this
;;
;; Frequency Response
;; ----------------------------------------------------------------
;; |****************************** |
;; | ***** |
@safiire
safiire / phase_accumulator.dsp
Last active December 22, 2015 19:19
A Phase Accumulator / Sine wave generator in Faust
import("math.lib");
Tau = 6.283185307179586;
Pi = 3.141592653589793;
Fs = SR;
oscillator(f, amplitude) = phase_accumulator(w) : sinf * amplitude with {
w = f / Fs * Tau;
sinf = ffunction(float sinf(float), <math.h>, "");
};
@safiire
safiire / template_magicks.cpp
Last active May 24, 2016 19:33
Using template meta-programming as a functional language in C++11
#include <iostream>
using namespace std;
////
// Pattern match on a list of types, recursively ask for the last one
template <typename Head, typename... Tail>
struct last {
using type = typename last<Tail...>::type;
};
@safiire
safiire / c++11_example.cpp
Last active May 25, 2016 00:23
Messing around with C++11
// C++11 is pretty neat so far.
//
// clang++ yay.cpp -o yay -std=c++11 -stdlib=libc++
#include <iostream>
#include <memory>
#include <vector>
#include <functional>
using namespace std;
@safiire
safiire / difference_equation.rb
Last active September 19, 2016 21:42
Modeling a filter's difference equation, so that it can invert itself.
#!/usr/bin/env ruby
# Really inefficient toy
####
## A little wrapper to make signals either with
## lambda functions or arrays.
class SignalFunction
def self.ramp
SignalFunction.new(lambda{|n| n < 0 ? 0r : n/1r})
end
@safiire
safiire / dual_number_derivative.rb
Last active October 2, 2016 12:18
Simulataneously calculate a function and it's derivitive using Dual Numbers
require 'matrix'
## Note: In ruby the notation x**n means x raised to the nth power
##
## In linear algebra, Dual numbers are in the form:
## a + bε
##
## Where ε is what is called nilpotent, you can think of
## ε as an infinitesimal (sort of kinda), like how they used to do Calculus in the
## olden days.

Keybase proof

I hereby claim:

  • I am safiire on github.
  • I am safiire (https://keybase.io/safiire) on keybase.
  • I have a public key whose fingerprint is 06DC 5901 70E2 350A 4BCE 7C1D 9EFD 7244 59CC 7EB0

To claim this, I am signing this object:

@safiire
safiire / kleisli.rb
Created December 16, 2016 03:06
Kleisli Gem is pretty awesome
require 'kleisli'
def do_lots(count)
(0..count).reduce(0){|sum, value| sum + value }
end
future = Future(100000000) >-> value {
Future {
do_lots(value.call)
} >-> big_sum {
@safiire
safiire / parse_json.hs
Created January 17, 2017 13:57
Parsing JSON in Haskell
{-# LANGUAGE OverloadedStrings, DeriveGeneric #-}
import Data.Text (Text)
import Data.Aeson
import GHC.Generics
import qualified Data.ByteString.Lazy as B
data Person =
Person { first :: !Text
, last :: !Text
@safiire
safiire / rc_filter_simulation.jl
Last active July 19, 2017 00:54
Same old RC Filter Simulation in Julia
abstract PassiveComponent
type Resistor <: PassiveComponent
value::Complex{Float64}
end
type Capacitor <: PassiveComponent
value::Complex{Float64}
end