<commit_before>`                                                                                                                 /*|*/ require = require('../Library/cov_require.js')(require)`
require('./utilities.coffee').infect global

uuid = require 'uuid'
util = require 'util'


module.exports =
   Paws = new Object

{debugging} = require('./additional.coffee')
debugging.inject Paws

# Core data-types
# ---------------
Paws.Thing = Thing = parameterizable class Thing
   constructor: constructify(return:@) (elements...)->
      @id = uuid.v4()
      @metadata = new Array
      @push elements... if elements.length
      
      
      @metadata.unshift undefined if @_?.noughtify != no
   
   rename: (name)-> @name = name ; return this
   
   # Construct a generic ‘key/value’ style `Thing` from a JavaScript `Object`-representation thereof.
   # These representations will have JavaScript strings as the keys (which will be converted into the
   # `Label` of a pair), and a Paws `Object`-type as the values.
   # 
   # For instance, given `{foo: thing_A, bar: thing_B}` will be constructed into the following:
   #    
   #    (, (, ‘foo’, thing_B), (, ‘bar’, thing_B))
   # 
   # The ‘pair-ish’ values are always owned by the generated structure; as are, by default, the objects
   # passed in. The latter is overridable with `.with(responsible: no)`.
   # 
   # @option responsible: Whether to mark the structure as `responsible` for the objects passed in.
   #---
   # TODO: Make recursive. Don't know how that didn't happen in the first place.
   # TODO: Support functions, so this can replace µPaws' applyGlobals.
   @construct: (representation)->
      members = for key, value of representation
         value = Alien.synchronous value if _.isFunction value
         value = @construct value unless value instanceof Thing
         value.rename key if @_?.names
         relation = Relation(value, @_?.responsible ? yes)
         Thing.pair( key, relation ).responsible()
      
      return Thing members...
   
   # XXX: Defined later, in `reactor.coffee`. These definitions have to be deferred, because
   #      `Execution` isn't defined yet.
   receiver: undefined
   
   at: (idx)->       @metadata[idx]?.to
   set: (idx, to)->  @metadata[idx] = Relation.from to
   
   push: (elements...)->
      @metadata = @metadata.concat Relation.from elements
   pop: ->
      @metadata.pop()
   shift: ->
      noughty = @metadata.shift()
      result = @metadata.shift()
      @metadata.unshift noughty
      result
   unshift: (other)->
      # TODO: include-noughtie option
      noughty = @metadata.shift()
      @metadata.unshift other
      @metadata.unshift noughty
   
   compare: (to)-> to == this
   
   # Creates a copy of the `Thing` it is called on. Alternatively, can be given an extant `Thing`
   # copy this `Thing` *to*, over-writing that `Thing`'s metadata. In the process, the
   # `Relation`s within this relation are themselves cloned, so that changes to the new clone's
   # responsibility don't affect the original.
   clone: (to)->
      to ?= new Thing.with(noughtify: no)()
      to.metadata = @metadata.map (rel)-> rel?.clone()
      
      to.name = @name unless to.name?
      
      return to
   
   toArray: (cb)-> @metadata.map (rel)-> (cb ? identity) rel?.to
   
   # This implements the core algorithm of the default jux-receiver; this algorithm is very
   # crucial to Paws' object system:
   # 
   # Working through the metadata in reverse, select those items whose *first* (not the naughty;
   # subscript-1) item `compare()`s truthfully to the searched-for key. Return them in the order
   # found (thus, “in reverse”), such that the latter-most item in the metadata that was found to
   # match is returned as the first match. For libside purposes, only this (the very latter-most
   # matching item) is used.
   #
   # Of note, in this implementation, we additionally test *if the matching item is a pair*. For
   # most *intended* purposes, this should work fine; but it departs slightly from the spec.
   # We'll see if we keep it that way.
   #---
   # FIXME: The only place that this can reasonably be used, the default-receiver, can't even use
   #        this as it's not defined in this file. Refactor me.
   find = (key)->
      # TODO: Sanity-check `key`
      results = @metadata.filter (rel)->
         rel?.to?.isPair?() and key.compare rel.to.at 1
      _.pluck(results.reverse(), 'to')
   
   #---
   # (Convenience method to `find` from nukespace. Accepts JavaScript primitives as keys.)
   # TODO: `raw` option, to return the `Relation`s, instead of the wrapped `Thing`s
   find: (key)->
      key = new Label(key) unless key instanceof Thing
      find.call this, key
   
   # TODO: Figure out whether pairs should be responsible for their children
   @pair: (key, value)->
      new Thing(Label(key), value)
   isPair:   -> @metadata[1] and @metadata[2]
   keyish:   -> @at 1
   valueish: -> @at 2
   
   responsible:   -> new Relation this, yes
   irresponsible: -> new Relation this, no

Paws.Relation = Relation = parameterizable delegated('to', Thing) class Relation
   # Given a `Thing` (or `Array`s thereof), this will return a `Relation` to that thing.
   # 
   # @option responsible: Whether to create new relations as `responsible`
   @from: (it)->
      if it instanceof Relation<commit_msg>(- re API) Re-arranging #find<commit_after>`                                                                                                                 /*|*/ require = require('../Library/cov_require.js')(require)`
require('./utilities.coffee').infect global

uuid = require 'uuid'
util = require 'util'


module.exports =
   Paws = new Object

{debugging} = require('./additional.coffee')
debugging.inject Paws

# Core data-types
# ---------------
Paws.Thing = Thing = parameterizable class Thing
   constructor: constructify(return:@) (elements...)->
      @id = uuid.v4()
      @metadata = new Array
      @push elements... if elements.length
      
      @metadata.unshift undefined if @_?.noughtify != no
   
   rename: (name)-> @name = name ; return this
   
   # Construct a generic ‘key/value’ style `Thing` from a JavaScript `Object`-representation thereof.
   # These representations will have JavaScript strings as the keys (which will be converted into the
   # `Label` of a pair), and a Paws `Object`-type as the values.
   # 
   # For instance, given `{foo: thing_A, bar: thing_B}` will be constructed into the following:
   #    
   #    (, (, ‘foo’, thing_B), (, ‘bar’, thing_B))
   # 
   # The ‘pair-ish’ values are always owned by the generated structure; as are, by default, the objects
   # passed in. The latter is overridable with `.with(responsible: no)`.
   # 
   # @option responsible: Whether to mark the structure as `responsible` for the objects passed in.
   #---
   # TODO: Support functions, so this can replace µPaws' applyGlobals.
   @construct: (representation)->
      members = for key, value of representation
         value = Alien.synchronous value if _.isFunction value
         value = @construct value unless value instanceof Thing
         value.rename key if @_?.names
         relation = Relation(value, @_?.responsible ? yes)
         Thing.pair( key, relation ).responsible()
      
      return Thing members...
   
   # XXX: Defined later, in `reactor.coffee`. These definitions have to be deferred, because
   #      `Execution` isn't defined yet.
   receiver: undefined
   
   at: (idx)->       @metadata[idx]?.to
   set: (idx, to)->  @metadata[idx] = Relation.from to
   
   push: (elements...)->
      @metadata = @metadata.concat Relation.from elements
   pop: ->
      @metadata.pop()
   shift: ->
      noughty = @metadata.shift()
      result = @metadata.shift()
      @metadata.unshift noughty
      result
   unshift: (other)->
      # TODO: include-noughtie option
      noughty = @metadata.shift()
      @metadata.unshift other
      @metadata.unshift noughty
   
   compare: (to)-> to == this
   
   # Creates a copy of the `Thing` it is called on. Alternatively, can be given an extant `Thing`
   # copy this `Thing` *to*, over-writing that `Thing`'s metadata. In the process, the
   # `Relation`s within this relation are themselves cloned, so that changes to the new clone's
   # responsibility don't affect the original.
   clone: (to)->
      to ?= new Thing.with(noughtify: no)()
      to.metadata = @metadata.map (rel)-> rel?.clone()
      
      to.name = @name unless to.name?
      
      return to
   
   # This implements the core algorithm of the default jux-receiver; this algorithm is very
   # crucial to Paws' object system:
   # 
   # Working through the metadata in reverse, select those items whose *first* (not the naughty;
   # subscript-1) item `compare()`s truthfully to the searched-for key. Return them in the order
   # found (thus, “in reverse”), such that the latter-most item in the metadata that was found to
   # match is returned as the first match. For libside purposes, only this (the very latter-most
   # matching item) is used.
   #
   # Of note, in this implementation, we additionally test *if the matching item is a pair*. For
   # most *intended* purposes, this should work fine; but it departs slightly from the spec.
   # We'll see if we keep it that way.
   #---
   # TODO: `raw` option, to return the `Relation`s, instead of the wrapped `Thing`s
   find: (key)->
      key = new Label(key) unless key instanceof Thing
      results = @metadata.filter (rel)->
         rel?.to?.isPair?() and key.compare rel.to.at 1
      _.pluck(results.reverse(), 'to')
   
   toArray: (cb)-> @metadata.map (rel)-> (cb ? identity) rel?.to
   
   # TODO: Figure out whether pairs should be responsible for their children
   @pair: (key, value)->
      new Thing(Label(key), value)
   isPair:   -> @metadata[1] and @metadata[2]
   keyish:   -> @at 1
   valueish: -> @at 2
   
   responsible:   -> new Relation this, yes
   irresponsible: -> new Relation this, no

Paws.Relation = Relation = parameterizable delegated('to', Thing) class Relation
   # Given a `Thing` (or `Array`s thereof), this will return a `Relation` to that thing.
   # 
   # @option responsible: Whether to create new relations as `responsible`
   @from: (it)->
      if it instanceof Relation
{'fraction_non_alphanumeric': 0.11403678606001937, 'fraction_numerical': 0.0013552758954501452, 'mean_word_length': 2.5971448467966574, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4475889', 'n_tokens_mistral': 3029, 'n_tokens_neox': 2932, 'n_words': 1136}