Skip to content

Instantly share code, notes, and snippets.

View djellemah's full-sized avatar
🤔

John Anderson djellemah

🤔
View GitHub Profile
@djellemah
djellemah / NicerEnv.fs
Created September 20, 2022 17:15
get environment variables in F#
let dict_map (dict : System.Collections.IDictionary) =
let values = ref (Map<'a,'b> [])
for obj in dict do
let de = obj :?> System.Collections.DictionaryEntry
values := Map.add (de.Key :?> 'a) (de.Value :?> 'b) !values
!values
System.Environment.GetEnvironmentVariables() |> dict_map<string,string>
@djellemah
djellemah / mview.rb
Created January 24, 2017 07:46
gtk2 sample with ruby-2.4
#! /usr/bin/env ruby
# Some of the first ruby I wrote, in 2005. So it's not very idiomatic :-|
# GDK_POINTER_MOTION_HINT_MASK
require 'gtk2'
require 'pango'
require 'mathn'
require 'getoptlong'
$ ./stap -p2 --vp 04 -e 'probe process("/bin/ls").function("main") { }'
Extracting build ID.
blacklist regexps:
blfn: ^(.^)$
blfn_ret: ^(_start)$
blfile: ^(.^)$
blsection: ^(.^)
dwarf_builder::build for /bin/ls
parse 'main', func 'main'
pattern '/bin/ls' matches module '/bin/ls'
== id ==
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel)
== stap -V ==
Systemtap translator/driver (version 2.9/0.163, non-git sources)
Copyright (C) 2005-2015 Red Hat, Inc. and others
This is free software; see the source for copying conditions.
enabled features: AVAHI BOOST_SHARED_PTR BOOST_STRING_REF LIBXML2 NLS NSS TR1_UNORDERED_MAP
== which stap ==
/usr/bin/stap
== cat /root/.systemtap/rc ==
@djellemah
djellemah / pattern_match.rb
Last active May 24, 2016 07:09
ruby pattern matching using refinements
class MatchIt
class Any
def ===( other ) true end
end
I = Any.new
module PatternMatch
refine Array do
def ===( other )
@djellemah
djellemah / pmap.rb
Last active March 19, 2016 11:07
Enumerable#pmap and Enumerable#pfind using SizedQueue and a simple future
require_relative 'waitron' # Waitron is a simple future
module Enumerable
# Return the first item for which bloc returns truthy.
# Obviously there's indeterminacy if there are several such items in the collection.
# This works best when bloc is waiting for something. Because GVL.
# Obviously bloc should be careful about mutable shared state
def pfind( threads: 4, &bloc )
q = SizedQueue.new threads
RD = React.DOM
class @ThingSearchResults extends React.Component
@defaultProps =
things: []
@propTypes:
select_handler: React.PropTypes.func
deselect_handler: React.PropTypes.func
@djellemah
djellemah / queue_multiplex.rb
Last active March 21, 2016 18:00
blocking Queue.multiplex in Ruby
require 'thwait'
# Synchronous hand-off between two threads.
# Both threads will block until a transfer is successful.
class Rendevous
def initialize
@mutex = Mutex.new
@send_flag = ConditionVariable.new
@recv_flag = ConditionVariable.new
@container = []
@djellemah
djellemah / one_time.rb
Last active March 21, 2016 12:00
One-shot thread-safe value store with blocking semantics.
# One-shot thread-safe value store with blocking explicit semantics, aka
# Future/Promise/IVar https://en.wikipedia.org/wiki/Futures_and_promises.
#
# In a sense, it's a state machine pattern for 3 states (fresh/unset,
# valued/set and exception). Apparently this is the same set of states as the
# PromiseA+ specification. Who knew?
#
# It can be initialised with a value, or the value can be assigned later on.
#
# You use it like this: