Skip to content

Instantly share code, notes, and snippets.

@kwijibo
Last active December 14, 2015 07:59
Show Gist options
  • Save kwijibo/3328611de27fcd43bcef to your computer and use it in GitHub Desktop.
Save kwijibo/3328611de27fcd43bcef to your computer and use it in GitHub Desktop.
functional program as triples in turtle (an ugly reinvention of Prolog?)
@prefix R: <npm:ramda/0.18.0> . #(made up) npm protocol to define the module as a namespace
@prefix r: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix : <#> . #this will be the namespace defining the lang-specific terms like :Text, :Fun1
@prefix owl: <#...> . #whatever the owl prefix is...
#functions are relationships, aka predicates, aka properties, between inputs and outputs
:STDOUT <app> :STDIN .
#owl:propertyChainAxiom is a composition of properties/predicates
<app> owl:propertyChainAxiom (
<wordSplit>
<countItems>
<sortTuplesByLastDescending>
<tuplesToTable>
)
# types indicate the arity of the function
<wordSplit> a :Fun1
; <split> "/\w|\s/" #this function is a partially applied version of <split>
; r:domain :Text # input type checking
; r:range :List # output type checking
.
<split> :sig (:Regex :Text :List ) . # a better way of doing type declaration?
<countItems> a :Fun1
; <countItemsInit> :EmptyDict
; r:domain :List
; r:range :Dict
.
<countItemsInit> a :Fun2
; <reduce> <itemCounter>
.
<reduce> a :Fun3
; r:domain :Fun2
; r:range :Fun2
.
<sortTuplesByLastDescending> a :Fun1
; <sort> [ <descSortBy> 1 ]
.
<tuplesToTable> owl:propertyChainAxiom (
[R:map [R:join " | " ]] #blank nodes can define predicates when used in "object position"
[R:join "\n"] # `R:` refers to ramda module defined in @prefix at the top
)
#implementations of base functions in Ecmascript 6
<split> :es """sep => str => str.split(sep)""" .
<itemCounter> :es """(accum={}, item) => { accum[item] = accum[item]++ || 1; return accum }""" .
<reduce> :es """func=>accum=>list=> list.reduce(func,accum)""" .
<toPairs> :es """o => Object.keys(o).map(i=>[i,o[i]])""" .
<sort> :es """func => list => list.sort(func) """ .
<descSortBy> :es """index=>(a,b)=>a[index]<b[index]""" .
@kwijibo
Copy link
Author

kwijibo commented Dec 14, 2015

Features of Turtle-as-a-programming-language:

  1. Support for Integers, Floats, URIs, user-defined literal datatypes, Lists, K:Vs, Graphs
  2. Order not significant
  3. Namespacing
  4. Functions can be typed
  5. Extensible metadata & documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment