Skip to content

Instantly share code, notes, and snippets.

View kingsleyh's full-sized avatar
😍
Coding

Kingsley Hendrickse kingsleyh

😍
Coding
View GitHub Profile
@kingsleyh
kingsleyh / ArrayExtra.mint
Last active August 31, 2018 21:14
Array.Extra
module Array.Extra {
/*
Map over a nested array and then flatten:
[[1,2],[1,5]]
Array.Extra.flatMap((a : Array(Number) : Array(Number) => { [Array.max(n)] }) == [2,5]
*/
fun flatMap (func : Function(a, Array(b)), array : Array(a)) : Array(b) {
Array.map(func, array)
|> Array.Extra.concat()
}
component Slider {
property onChange : Function(Number, Void) = (value : Number) : Void => { void }
property disabled : Bool = false
property max : Number = 100
property value : Number = 0
property step : Number = 1
property min : Number = 0
state sliderPos : String = Number.toString(value)
style rangeSlider {
require "ssh2"
SSH2::Session.open("localhost", 22) do |session|
session.login("test", "password")
session.open_session do |ch|
ch.request_pty("vt100")
ch.shell
session.blocking = false
buf_space = uninitialized UInt8[1024]
@kingsleyh
kingsleyh / ensure_ascii_compatible_encoding.rb
Created January 22, 2018 18:03
ensure_ascii_compatible_encoding
def ensure_ascii_compatible_encoding(string, options = nil)
if string.encoding.ascii_compatible?
string
else
string.encode(Encoding::UTF_8, options || {:invalid => :replace, :undef => :replace})
end
end
@kingsleyh
kingsleyh / crystal.cr
Created January 22, 2018 12:24
crystal wierdness
base = 1
[1,2,3,4,5,6,7].each do |n|
base *= 256
p base
end
#256
#65536
#16777216
#0
@kingsleyh
kingsleyh / a_my_code.cr
Created January 6, 2018 21:51
Iterator issues
p my_cursor
#<RethinkDB::Cursor:0x10bead8a0
@index=1,
@response=
#<RethinkDB::Connection::Response:0x10beb0a50
@b=nil,
@e=nil,
@n=[],
@p=nil,
@kingsleyh
kingsleyh / a.cr
Created January 6, 2018 17:00
crystal types
result = repo.user.findByEmail(user.value["email"])
p typeof(result.value) #(Iterator::Stop | RethinkDB::QueryResult | String)
p result.value.is_a?(RethinkDB::QueryResult) #true
result.should be_a(DB::Success(RethinkDB::QueryResult)) # fails with error below
Failures:
1) Repository Repository::User should find a user by email
Failure/Error: result.should be_a(DB::Success(RethinkDB::QueryResult))
@kingsleyh
kingsleyh / g.cr
Created January 2, 2018 16:53
generics
def onParams(context, klass : T, &block) forall T
result = klass.parse(contextToJson(context))
p typeof(result)
if result.is_a?(T)
yield result
else
HTTPCodes.notFound(context, {message: result})
end
end
@kingsleyh
kingsleyh / cr1.cr
Last active January 1, 2018 17:38
crystal
def isLoggedIn(context, repo)
sessionId = context.session.string?("userId")
if sessionId
user = repo.findUserByUserId(sessionId)
case user
when DB::Success
groupId = user.value["activeChannel"].as_h.fetch("groupId", "")
channelId = user.value["activeChannel"].as_h.fetch("channelId", "")
@kingsleyh
kingsleyh / Main.elm
Created November 22, 2017 15:47
Ksenia Number Game in Elm
module Main exposing (..)
import Html exposing (Html, button, div, input, p, program, text)
import Html.Attributes exposing (placeholder, type_)
import Html.Events exposing (onClick, onInput)
-- MODEL