Data Visibility Abstraction @stuartsierra
“Being abstract is something profoundly different from being vague... The purpose of abstraction is not to be vague, but to create a new semantic level in which one can be absolutely precise.” -Edsger W. Dijkstra
BASIC
10 FOR I = 1 TO 5 20 PRINT I 30 NEXT I RUN 1 2 3 4 5
SUB FOO (parameters) ' ... do whatever ... END SUB FUNCTION BAR (parameters) ' ... do whatever ... BAR = ' return value END FUNCTION
C++
http://abstrusegoose.com/249
Perl
PERL(1) Perl Programmers Reference Guide PERL(1) NAME perl - Practical Extraction and Report Language SYNOPSIS Overview perl Perl overview (this section) perlintro Perl introduction for beginners perltoc Perl documentation table of contents Tutorials perlreftut Perl references short introduction perldsc Perl data structures intro perllol Perl data structures: arrays of arrays perlrequick Perl regular expressions quick start perlretut Perl regular expressions tutorial perlboot Perl OO tutorial for beginners perltoot Perl OO tutorial, part 1
#!/usr/bin/env perl my $user = { name => "Stuart", age => 15, langs => [ "BASIC", "C++", "Perl" ] }; print $user->{'langs'}[2];
Tie::File(3pm) Perl Programmers Reference Guide Tie::File(3pm) NAME Tie::File - Access the lines of a disk file via a Perl array SYNOPSIS # This file documents Tie::File version 0.97 use Tie::File; tie @array, 'Tie::File', filename or die ...; $array[13] = 'blah'; # line 13 of the file is now 'blah' print $array[42]; # display line 42 of the file $n_recs = @array; # how many records are in the file? $#array -= 2; # chop two records off the end for (@array) { s/PERL/Perl/g; # Replace PERL with Perl everywhere }
XML
<user> <name>Stuart</name> <age>15</age> <langs> <lang>BASIC</lang> <lang>C++</lang> <lang>Perl</lang> <lang>XML</lang> </langs> </user>
use XML::DOM; my $parser = new XML::DOM::Parser; my $doc = $parser->parsefile ("file.xml"); # print all HREF attributes of all CODEBASE elements my $nodes = $doc->getElementsByTagName ("CODEBASE"); my $n = $nodes->getLength; for (my $i = 0; $i < $n; $i++) { my $node = $nodes->item ($i); my $href = $node->getAttributeNode ("HREF"); print $href->getValue . "\n"; }
<xsl:template match="/doc:article/doc:info"/> <xsl:template match="doc:link"> <xsl:element name="ulink" namespace="http://docbook.org/ns/docbook"> <xsl:attribute name="url" namespace="http://docbook.org/ns/docbook"> <xsl:value-of select="@xlink:href"/> </xsl:attribute> </xsl:element> </xsl:template> <xsl:template match="doc:footnote"> <xsl:element name="footnote" namespace="http://docbook.org/ns/docbook"> <xsl:apply-templates select="*"/> </xsl:element> </xsl:template> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template>
#!/usr/bin/env bash saxon one.xslt site.xml t1.xml saxon two.xslt t1.xml t2.xml saxon three.xslt t2.xml t3.xml saxon four.xslt t3.xml index.html
Common Lisp
http://gigamonkeys.com/
(defun breadth-first-search (start) (let ((open (list start)) ; the list of nodes to be examined (closed (list)) ; the list of nodes already examined (steps 0) ; number of iterations (expanded 0) ; total number of nodes expanded (stored 0)) ; max number of nodes stored at any one time (loop while open do (let ((x (pop open))) (when (finished? x) (return (format nil "Found ~a in ~a steps. Expanded ~a nodes, stored a maximum of ~a nodes." x steps expanded stored))) (incf steps) (pushnew x closed :test #'equal) (let ((successors (successors x))) (incf expanded (length successors)) (setq successors (delete-if (lambda (a) (or (find a open :test #'equal) (find a closed :test #'equal))) successors)) (setq open (append open successors)) (setq stored (max stored (length open))))))))
The Quagmire of Convenience access fare-utils my-util Alexandria Formlets pergamum arnesi fukacl qtility aromyxo hu.dwim.util rutils bknr-utils incf-cl s-utils ch-util JARW SCLF cl-configuration kmrcl umpa-lumpa cl-jpl-util mccme-helpers ytools cl-utilities metatilities CLLIB metatilities-base common-idioms monkeylib- de.setf.utility utilities dso-util mop-utils
;; Association list (setq user '((name . "Stuart") (age . 24) (langs . ("XSLT" "Perl" "Lisp"))) ;; Hash table (setq user (make-hash-table)) (setf (gethash 'name user) "Stuart") (setf (gethash 'age user) 24) (setf (gethash 'langs user) '("XSLT" "Perl" "Lisp"))
(defcenum svtype :null ; undef :iv ; Scalar (integer) :nv ; Scalar (double) :rv ; Scalar (reference) :pv ; Scalar (string) :pviv ; pointer to an IV (used in hashes) :pvnv ; pointer to an NV (used in hashes) :pvmg ; blessed or magical scalar :pvbm ; ?? :pvlv ; ?? :pvav ; Array :pvhv ; Hash :pvcv ; Code reference :pvgv ; typeglob (possibly file handle) :pvfm ; ?? :pvio) ; an I/O handle?
(defun call-perl (function return-type methodp &rest args) (need-perl) (perl-scope (pushmark) (push-mortals-on-stack args) (get-stack-by-type return-type (funcall (if (stringp function) #'perl-call-function ;; either scalar string or code reference #'perl-call-scalar) function (calling-flags return-type methodp)))))
Ruby
class Company < ActiveRecord::Base include Auditable belongs_to :group has_many :profiles, :dependent => :destroy has_many :users, :dependent => :destroy has_many :subscriptions, :dependent => :destroy, :order => :service_id has_many :services, :through => :subscriptions has_many :statistics, :dependent => :destroy accepts_nested_attributes_for :subscriptions, :allow_destroy => true, :reject_if => lambda{|attrs| attrs[:_create] == '0'} accepts_nested_attributes_for :users, :allow_destroy => true after_update :ensure_users_match validates_presence_of :name validates_uniqueness_of :name validate :presence_of_subscriptions default_scope :order => :name end
doc = Hpricot.parse("file.xml") doc / "/html/body//p//img" doc / "html > body > p img" doc / :html / :body / :p / :img
pidgin a grammatically simplified form of a language, used for communication between people not sharing a common language.
Clojure
(def user {:name "Stuart" :age 25 :langs ["Lisp" "Ruby" "Clojure"]}) (-> user :langs last) ;;=> "Clojure"
user=> (inspect-tree user)
(map function set-of-maps) (map function map-of-maps) (map function list-of-vectors) (map function (range)) ; all integers (map function "characters in string") (map function (file-seq directory)) (map function (line-seq file)) (map function (xml-seq xml-tree)) (map function (resultset-seq query))
(def primes (letfn [(next-prime [known-primes n] (lazy-seq (if (some #(zero? (rem n %)) known-primes) (next-prime known-primes (inc n)) (cons n (next-prime (conj known-primes n) (inc n))))))] (next-prime [] 2))) (map #(inc (* 2 %)) primes) ;;=> (5 7 11 15 23 27 35 39 47 59 ...)
user=> (def editor (d/entity db editor-id)) user=> editor ;;=> {:db/id 17592186045425} user=> (:user/firstName editor) ;;=> "Edward" user=> (:comment/_author editor) ;;=> [{:db/id 17592186045429} ...]
(defn complex-process [] (let [result (computation (get-input)] (if (condition? result) (launch-missile) (erase-hard-drive))))
user=> (complex-process) ;;=> nil
(defn gather-information [state] (assoc state :analysis (computation (:input state))) (defn make-decision [state] (assoc state :response (if (condition? (:analysis state)) :launch-missile :erase-hard-drive)))
user=> (make-decision {:input "trustno1"}) ;;=> {:input "trustno1" ;; :analysis :security-breach ;; :response :erase-hard-drive}
(defn take-action [state] (case (:response state) :launch-missile (launch-missile) :erase-hard-drive (erase-hard-drive))) (defn complex-process [initial-state] (-> initial-state gather-information make-decision take-action))
(defn log [state] (logging/info state) state) (defn complex-process [initial-state] (-> initial-state gather-information make-decision log take-action))
Input Convert Data Data Effect or Data Convert Output
Enter Enter Enter Data Data Data Request Interceptor Interceptor Interceptor One T wo Three Leave Leave Leave Data Data Data Response
http://pedestal.io
Recommend
More recommend