Skip to content

Instantly share code, notes, and snippets.

@HakimCassimallyBBC
HakimCassimallyBBC / num.py
Created November 10, 2020 09:30
Rather "un-pythonic" FP-ish (but with explicit array access) snippet, discovered in an algorithm book
import functools
def roman_to_integer(s: str) -> int:
T = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
return functools.reduce(
lambda val, i: val + (-T[s[i]] if T[s[i]] < T[s[i+1]] else T[s[i]]),
reversed(range(len(s)-1)), T[s[-1]])
$('tr.memberships__table-row')
.filter( function () { return $(this).find('td.memberships__td-user-status--active').length })
.map( function () { return $(this).find('td.memberships__td-user').get(0).title })
.get()
.sort()
.join("\n")
@HakimCassimallyBBC
HakimCassimallyBBC / foo.clj
Created October 24, 2017 15:20
clojure zipper attributes?
(defn nodes-to-bequeath [parent]
(xml-> (zipper/xml-zip parent)
zipper/children
#(if (get-in % [:attrs ::pm/inherit]) % nil)))
@HakimCassimallyBBC
HakimCassimallyBBC / scratch.clj
Created July 25, 2017 11:37
FSM programme flow, allowing pure single-stepping through a branching pipeline
(ns scratch.scratch
(:gen-class))
(defn effect? [[k f]]
(= \! (last (name k))))
(defn if? [[k f]]
(= :if k))
(defn step [model fsm]
#!/bin/bash
# Usage:
#
# nit-parallel [cosmos-running-instances args ...] -- [parallel args ...]
#
# e.g.
#
# ### No piped input: run command on all hosts
# nit-parallel -e test pm-nitro-test-1 -- hostname
$ function map () { while read -r VAR; do eval "$@"; done }
$ seq 5 | map echo '{{{$VAR}}}'
{{{1}}}
{{{2}}}
{{{3}}}
{{{4}}}
{{{5}}}
use strict; use warnings;
use XML::LibXML;
use XML::LibXML::XPathContext;
my $xml = <<'__EOI__';
<par xmlns:bar="www.bar.com"><bar:foo/></par>
__EOI__
my $parser = XML::LibXML->new();
@HakimCassimallyBBC
HakimCassimallyBBC / mysql_splitdump.sh
Created March 8, 2017 11:46
slightly modified mysql_splitdump.sh
#!/bin/bash
####
# Split MySQL dump SQL file into one file per table
# based on http://blog.tty.nl/2011/12/28/splitting-a-database-dump
# from https://gist.github.com/jasny/1608062
####
if [ $# -lt 1 ] ; then
echo "USAGE $0 DUMP_FILE"
@HakimCassimallyBBC
HakimCassimallyBBC / process_file_queue.pl
Created March 6, 2017 09:16
Sketch for processing a queue of files (several instances can be launched in parallel)
use strict;
use warnings;
use File::Spec;
my $in = 'in';
my $out = 'out';
sub process {
my ($in, $out, $callback) = @_;
opendir (my $DIR, $in) or die "Failed to open dir $in: $!";
@HakimCassimallyBBC
HakimCassimallyBBC / gist:10fdaec1d1907e758e92403933ada0c8
Created February 23, 2017 09:38
and-> function to add a parenthesis to a threading form
boot.user=> (defn and->> [f value] (doto value f))
#'boot.user/and->>
boot.user=> (defn and-> [value f] (doto value f))
#'boot.user/and->
;; use the appropriate and->(>) form for your threading macro
boot.user=> (-> 1 (and-> println))
1
1