Instantly share code, notes, and snippets.

🚀
Lightspeed

# Roger Qiu CMCDragonkai

🚀
Lightspeed
Created October 27, 2014 09:07
PHP: Dynamic Fibonnaci O(n)
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

Created October 27, 2014 09:08
Nix: Dynamic Fibonnaci in Functional Style O(n)
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
 let fib = number: fib' number 1 1; fib' = number: first: second: if number == 0 then first else fib' (number - 1) second (first + second); # the second becomes the new first, the (first + second) becomes the new second in fib 5
Created October 27, 2014 09:21
Elixir: Converting a bitstring to integer. Basically converting base 1 to base 2.
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
 # Note that you can also do things like for << b::2 <- <<1::1, 1::1, 1::1, 0::1>> >>, do: b # It's pretty cool, it makes sure the b variable is composed of 2 bits. So it will segment the bitstring to represent and have create a b for every 2 bits. # Binary Comprehension over a bitstring of size 1 bits list = for << b::1 <- <<1::1, 1::1, 1::1, 0::1>> >>, do: b # The Goal is the turn <<1::1, 1::1, 1::1, 0::1>> into a base 2 integer, which would be 14 # POWER is like 2^2 which means 2*2. So if x^y. Then there y recursions of x * x. defmodule Math do
Created October 27, 2014 09:28
Nix: Lazy Linked List Implementation using Nested Attribute Sets. This example specifically reifies the fibonnaci sequence into a lazy linked list.
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
 # nix lists are not lazy linked lists # we can however create lazy linked list using nested attribute sets, where the head is the node value and the tail is the nested cons # we've basically created a recursive union type: # type stream a = nil | { head = a; tail = stream a; } # it's basically a lazy linked list!!!! let # O(n) access of the linked list, it needs to unwrap the linked list as it goes deeper into the list streamElemAt = s: i:
Created October 27, 2014 09:32
Mercury: Append Predicate - this demonstrates argument unification
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
 %% this is a linked list append, which is O(n), because it needs to break up the list until the end, then add the list to be appended at the very end, then build up the list again by consing them! %% unlike loops, each step is a recursion, and I guess there might some sort of tail call optimisation that makes sure that a stack doesn't grow forever in functional languages :- pred append(list(T), list(T), list(T)). :- mode append(in, in, out) is det. :- mode append(out, out, in) is multi. append(Xs, Ys, Zs) :- ( Xs = [], Zs = Ys %% conjunction
Created October 27, 2014 09:51
Python: Depth First Search on a Adjacency List - returns a list of nodes that can be visited from the root node.
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
 def depth_first_search(neighbour_list, root_node): visited_nodes = set() stack = [root_node] while stack: node = stack.pop() if node not in visited_nodes: visited_nodes.add(node) stack.extend(set(neighbour_list[node]) - visited_nodes) return list(visited_nodes)
Last active August 29, 2015 14:08
PHP: Equal Needle Segregation. Given an array containing Xs and Ys, find they key index in which the number of Xs on the left is equal to the number of Ys on right, where X is some arbitrary needle and Y is any element that is not X. The key position is inclusive of Xs.
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

Created November 2, 2014 11:58
PHP: Does the turtle cross a point that it has already visited? (Possibly useful in a snake games)
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

Last active August 29, 2015 14:09
OMetaJS: FizzBuzz Compiler
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
 // our FizzBuzz language var code = "for every number from 1 to 100\ if the number is a multiple of 3 and it is a multiple of 5 then\ print \"FizzBuzz\"\ else if it is a multiple of 3 then\ print \"Fizz\"\ else if it is a multiple of 5 then\ print \"Buzz\"\ else\
Last active August 29, 2015 14:09
OMetaJS: List Of Higher Order
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
 ometa List { listOf :p = apply(p):head (',' apply(p))*:tail -> [head].concat(tail), listOfDigits = listOf('digit'):l -> l, END } var result = List.matchAll( '1,2,3,4,5', 'listOfDigits' );