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
class Prime | |
include Enumerable | |
def self.nth(index) | |
raise ArgumentError, 'index out of bound' if index <= 0 | |
new.take(index).last | |
end | |
def each(&block) | |
(2..Float::INFINITY).each do |i| |
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 self.prime?(num) | |
return num > 1 if num <= 3 | |
return false if (divisible?(num,2) || divisible?(num,3)) | |
# loop while (6*k-1)**2 <= num, or | |
# k <= (1+sqrt(num)/6) | |
max_k = (Math.sqrt(num)+1)/6.0 | |
return false if (1..max_k).any? {|k| divisible?(num,6*k+1) || divisible?(num,6*k-1)} | |
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
Show hidden characters
# Get Sublime Text to use your rvm ruby without hardcoding a `$USER`. | |
# | |
# Include the configurations below the commend in the appropriate file listed below: | |
# | |
# - OS X ST2: ~/Library/Application Support/Sublime Text 2/Packages/Ruby/Ruby.sublime-build | |
# - OS X ST3: ~/Library/Application Support/Sublime Text 3/Packages/User/Ruby.sublime-build | |
# - Linux ST2: ~/.config/sublime-text-2/Packages/Ruby/Ruby.sublime-build | |
# - Linux ST3: ~/.config/sublime-text-3/Packages/User/Ruby.sublime-build | |
{ |
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
// Bird's-eye view of overall production code | |
Cmac_Calculate() | |
{ | |
aes_handle = Aes128_Create(key, iv) | |
// Perform several technical operations (CmacOps) per the CMAC spec. | |
CmacOps_GenerateSubkeys(aes_handle, *subkey1, *subkey2) | |
//... | |
CmacOps_ApplyCbcMac(aes_handle, <relevant_parameters>) | |
//... |
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
// From production code | |
typedef struct AES_STRUCT * AES_HANDLE; | |
typedef struct | |
{ | |
char *key; | |
size_t key_len; | |
char *iv; | |
size_t iv_len | |
} CREATE_PARAMS; |
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
# Get the exercise name from the current directory | |
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME) | |
# Basic CMake project | |
cmake_minimum_required(VERSION 3.1.3) | |
# Name the project after the exercise | |
project(${exercise} CXX) | |
# Locate Boost libraries: unit_test_framework, date_time and regex |
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
// Example of non-lexical lifetimes for references in Rust | |
// | |
// References go out of scope at their last use, | |
// not at the end of a block. | |
fn main() { | |
example1(); | |
example2(); | |
example3(); | |
} |
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
# Docs: | |
# https://docs.python.org/3/library/functools.html#functools.reduce | |
import functools | |
# reduce(function, iterable) | |
# Applies function to each item of the sequence, left to right. | |
# Passes two arguments to the function: | |
# the current total | |
# the next item in the sequence |
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
// Uncomment /* */ blocks within functions to see compiler errors. | |
/* | |
Links to documentation | |
str, the string primitive: | |
https://doc.rust-lang.org/std/primitive.str.html | |
str, trait implementations: | |
https://doc.rust-lang.org/std/primitive.str.html#implementations |
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 initialState = 'inactive'; | |
const toggleMachine = Machine({ | |
id: 'toggle', | |
initial: initialState, | |
states: { | |
inactive: { // 'inactive' state | |
on: { // "on event, transition to state..." | |
TOGGLE: 'active' // on event 'TOGGLE', transition to 'active' | |
} |
OlderNewer