Skip to content

Instantly share code, notes, and snippets.

@msgodf
Last active April 3, 2016 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msgodf/ea29537203676d6393ce89209a8fb0a6 to your computer and use it in GitHub Desktop.
Save msgodf/ea29537203676d6393ce89209a8fb0a6 to your computer and use it in GitHub Desktop.
NB. Turn on tree formatting of sentences:
(9!:3) 4
1;2;3;4
NB. ┌─┬─┬─┬─┐
NB. │1│2│3│4│
NB. └─┴─┴─┴─┘
NB. Using { 'From' (http://www.jsoftware.com/help/dictionary/d520.htm)
0{1;2;3;4
NB. ┌─┐
NB. │1│
NB. └─┘
NB. Given a predicate `p` that elements must be less than three
p=: <&3
NB. This is the equivalent of filter:
(p #" |: ]) i. 5
NB. => 0 1 2
NB. The whitespace isn't required, so this can also be expressed as
(p#"|:]) i.5
NB. This relies on the composition rules for verb trains, in this case it's a fork of p, ", and ]
NB. ┌─ p
NB. │ ┌─ #
NB. ──┼─ " ─┴─ _ 1 _
NB. └─ ]
NB. It looks like transpose `|:` and rank `"` interact strangely here, so filter can also be written as
(p#"_])
NB. Also, "_ doesn't do anything here - so the final version is just
(p#])
NB. Although this might not be equivalent when filtering on higher rank nouns. And I'm not really sure how that would work anyway - I guess it would be a verb that takes another verb and a noun and produces a noun of the same rank, but with equal or smaller dimensions.
NB. I wonder whether / is actually interposing, or whether it's a real fold.
NB. I think it's a fold, here's an example using link `;`
;/i.4
NB. ┌─┬─┬─┬─┐
NB. │0│1│2│3│
NB. └─┴─┴─┴─┘
NB. I often want a predicate for the rank of a noun
has_rank=:=#&$
NB. And this can then be curried for predicates specific to different ranks
is_list =: 1 & has_rank
is_matrix =: 2 & has_rank
NB. Let's try word counting. Can define a multiline text string like this
example_text =: 0 : 0
this is a test
over
multiple lines
)
NB. Can break this into lines with
(< ;. _2) example_text
NB. ┌──────────────┬────┬──────────────┐
NB. │this is a test│over│multiple lines│
NB. └──────────────┴────┴──────────────┘
NB. As described at http://www.jsoftware.com/help/learning/17.htm
NB. The meaning of _2 for the right argument of ;. is that the words are to be terminated by occurrences of the last character in y (the space), and furthermore that the words do not include the spaces.
NB. And split each boxed line into words
(;: &. >) (< ;. _2) example_text
NB. ┌────────────────┬──────┬────────────────┐
NB. │┌────┬──┬─┬────┐│┌────┐│┌────────┬─────┐│
NB. ││this│is│a│test│││over│││multiple│lines││
NB. │└────┴──┴─┴────┘│└────┘│└────────┴─────┘│
NB. └────────────────┴──────┴────────────────┘
NB. And count the words in each line
(# & ;: &. >) (< ;. _2) example_text
NB. ┌─┬─┬─┐
NB. │4│1│2│
NB. └─┴─┴─┘
NB. We can also get boxes of words and lines:
;:;._2 example_text
NB. ┌────────┬─────┬─┬────┐
NB. │this │is │a│test│
NB. ├────────┼─────┼─┼────┤
NB. │over │ │ │ │
NB. ├────────┼─────┼─┼────┤
NB. │multiple│lines│ │ │
NB. └────────┴─────┴─┴────┘
NB. And count each line of this
#&;:;._2 example_text
NB. 4 1 2
NB. Can calculate the number of non-whitespace characters
# {: ;\ ;: ;. _2 example_text
NB. Can calculate the number of non-whitespace characters in each line
+/ |: # & > ;: ;. _2 example_text
NB. 11 4 13
NB. Calculate the average word length
(+/%#)(>&0#]),#&>;:;._2 example_text
NB. This can be broken up into the following
mean =: +/%#
remove_zeroes =: >&0#]
word_grid =: ;:;._2
count_cells =: #&>
flatten_grid =: ,
NB. To read from right to left as
mean remove_zeroes flatten_grid count_cells word_grid example_text
NB. Which could be more easily read in a Lisp-like parenthesised expression
(mean (remove_zeroes (flatten_grid (count_cells (word_grid example_text)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment