Skip to content

Instantly share code, notes, and snippets.

View KevinWMatthews's full-sized avatar
🦀
Studying Rust

Kevin W Matthews KevinWMatthews

🦀
Studying Rust
View GitHub Profile
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|
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
# 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
{
@KevinWMatthews
KevinWMatthews / cmac_aes_mock.c
Last active March 24, 2018 05:26
Overview and example of mocking an AES encryption library
// 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>)
//...
// 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;
@KevinWMatthews
KevinWMatthews / exercism-cpp-CMakeLists.txt
Last active August 30, 2018 16:23
CMakeLists.txt from Exercism's C++ track
# 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
@KevinWMatthews
KevinWMatthews / mutable_reference_scope.rs
Created June 28, 2019 16:31
Rust borrowing rules - reference scope and non-lexical lifetimes
// 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();
}
@KevinWMatthews
KevinWMatthews / reduce_demo.py
Created August 11, 2019 20:17
Reduce in Python
# 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
@KevinWMatthews
KevinWMatthews / rust-string-concat.rs
Last active September 16, 2019 14:48
String concatenation do's and dont's in Rust
// 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
@KevinWMatthews
KevinWMatthews / machine.js
Last active December 1, 2019 17:01
Generated by XState Viz: https://xstate.js.org/viz
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'
}