Skip to content

Instantly share code, notes, and snippets.

@harms
harms / PrimeFactorsTddJoke.ijs
Created February 14, 2011 17:45
An indulgence in sarcastic humor.
NB. The Prime Factor Kata, in J
NB. -- an exercise in humor --
NB. Inspired by http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata
NB. RED
testPrimeFactors =: 3 : 0
assert ($0) -: primefactors 1 NB. add test of 1
)
NB. GREEN
@harms
harms / gist:1444658
Created December 7, 2011 21:06
Counting characters by kind.
whitespace=: 9 10 32 { a.
vowel =: ( , toupper) 'aeiou'
consonant =: vowel -.~ a.{~ , 97 65 +"0 _ i.26
other =: a. -. whitespace, vowel, consonant
kind=: whitespace; vowel; consonant; other
count=: ([: +/ ] e. >@[ )" _1 _
@harms
harms / gist:1624764
Created January 17, 2012 04:47
J for directionally filtering out rows or columns of a matrix.
'byRow byColumn'=: <"0 s:' Row Column' NB. these two names may be any two distinct values
nix=: 2 :' #~"(1+n=byRow) -.@ u"(1+n=byColumn) '
NB. examples of use:
(2={.) nix byColumn i. 3 5
0 1 3 4
5 6 8 9
10 11 13 14
@harms
harms / POST_handler_sketch.ijs
Created March 8, 2012 01:33
J akin to a Scala Option example.
NB. preparatory
OrFailureReport=: adverb : ' u ` asFailure @. fails '
UnlessFailed=: adverb : ' u ` ] @. fails '
NotingUnknownParsingError=: ::((makeFailure 'Unknown error in parsing attempt.')"_)
FAILMARK=: < s:'`_|_' NB. arbitrary
makeFailure=: [: < FAILMARK ; < :[:
fails=: [:+./ [:flatten (FAILMARK e. ]) L:1
NB. main
@harms
harms / unshared.ijs
Created May 21, 2012 13:40
Example J solution to (Ruby) question asked by Bill Tozier
unshared=: (-. , -.~) &. setForm
setForm=: convert :. revert
convert=: [: ; (<@(j. i.@#))/.~
revert=: /:~ @ {. @ |: @: +.
A=. 1 2 2 3 3 4 5
B=. 2 3 3 3 3 4 5 6
A unshared B
1 2 3 3 6
@harms
harms / digexamples.ijs
Created September 4, 2012 05:40
Three versions of 'dig', moving away from statement-based conditionals.
NB. These snippets are involved with a J implementation of RCRPG: http://rosettacode.org/wiki/Category:RCRPG
NB. That solution is still being written, so has not yet been posted to that website.
NB. The exercise of eliminating control-statements was largely inspired by the conversation on Twitter with @Huperniketes that included this comment: https://twitter.com/Huperniketes/status/240917975228637185
NB. NOTE: The version posted earlier, with tacit definitions of verbs such as fail_tool, did not work. The final portion has been corrected to fix this.
NB. Here's the original version of the verb 'dig' (omitting definitions of included names)
dig=: 3 : 0
if. PC_equipped includes 1 Sledge do.
@harms
harms / test-isAceLowStraight
Created May 16, 2013 11:53
Example tests, applicable of 'isAceLowStraight'. Works in context of programs found at http://www.jsoftware.com/jwiki/TracyHarms/PokerHandsScoring
prep=: ranks @: makeHand
assert -. isAceLowStraight prep '6 D 4 H 3 C 2 S A C'
assert -. isAceLowStraight prep '6 H 5 D 4 H 3 C 2 S'
assert isAceLowStraight prep '5 D 4 H 3 C 2 S A C'
assert isAceLowStraight prep '5 D 4 D 3 D 2 D A D'