User:Hkumbum/sandbox
![]() | This is a user sandbox of Hkumbum. You can use it for testing or practicing edits. This is not the sandbox where you should draft your assigned article for a dashboard.wikiedu.org course. To find the right sandbox for your assignment, visit your Dashboard course page and follow the Sandbox Draft link for your assigned article in the My Articles section. |
Paradigm | Functional, Scripting |
---|---|
Designed by | Phil Freeman |
Developers | Phil Freeman, Gary Burgess, Hardy Jones,Harry Garrood |
First appeared | September 29, 2013 |
Stable release | v0.8.0 [1]
/ February 4, 2016 |
OS | Cross-platform |
License | MIT License |
Filename extensions | .purs |
Website | www |
Influenced by | |
Haskell, JavaScript, ML |
PureScript (/ˈpjʔr.skrʔpt/) is a Functional Programming language which compiles to JavaScript[2]. PureScript is a statically typed language and unlike other dynamically typed languages, in PureScript, types exist only at compile time and have no representation at run time. Programs which are correct will be given a type by the compiler, which illustrates its behavior. Conversely, programs which are not given any type are incorrect and will be rejected by the compiler.[3].One advantage of PureScript over JavaScript is that JavaScript does not support powerful abstractions, as a result it cannot utilize the full capability of functional programming and PureScript addresses this issue by allowing expressive, readable code.
History
[edit]Phil Freeman made the first Git commit of PureScript on September 29, 2013. There have been 45 releases with 2924 commits ( as of 15 February, 2016). On Feb 1, 2016, Phil released the latest version v0.8.0 - TIMESLIDES
Getting started
[edit]First, setup a working environment for PureScript development.
Following are the tools required:
Download the latest version of the PureScript binary from Github [5] and setup the environment variables. Try executing the commands below in command line to verify PureScript installation:
$psc
Following command is to install the Pulp command line tool[6].
$ npm install -g pulp
Now you have all tools required to write the first PureScript library[7].
Simple Hello World!
By using pulp init[8] command, create project in an empty directory.
$ mkdir my_pscript
$ cd my_pscript
$ pulp init
* Generating project skeleton in ~/my-pscript
$ ls
bower.json src test
Two directories src, test and a configuration file bower.json are created. src directory contains the source files and test directory contains the tests performed.
Now Modify the src/Main.purs file to create Hello World Program[9].
module Main where
import Control.Monad.Eff.Console
main = log "Hello, World!"
Now let's build and run the above code.
$ pulp run
* Building project in ~/my-pscript
* Build successful.
Hello, World!
Features
[edit]Below are some of the interesting features of PureScript.
Types and Type Inference
[edit]Types in PureScript supports type inference and are not similar to the data types in Java or C#. These types were inspired from ML and Haskell). Following is a simple example code which defines a number (no reference to the Number type anywhere in the code).[10]
iAmANumber =
let square x = x * x
in square 42.0
Pattern matching
[edit]Pattern matching is a technique that allows to code compact functions which express complex ideas. Algebraic data types is a similar feature and is closely related to pattern matching. Below is a Simple Pattern Matching function that calculates GCD of two numbers using Euclidean Algorithm.[11]
gcd :: Int -> Int -> Int
gcd n 0 = n
gcd 0 m = m
gcd n m = if n > m then gcd (n - m) m else gcd n (m - n)
Pattern matching allows to code simple and declarative cases. Functions work by pairing sets with results. Each line in the function is a case, expressions on left of equals sign are patterns. Each case is evaluated and the first case that matches is returned.
There are various types of patterns like Integer literal, Variable, Wildcard and Boolean literal patterns.
Simple FFI
[edit]FFI(Foreign Function Interface) is a feature provided by PureScript that enables communication between PureScript and JavaScript code. FFI is very flexible and it allows developer to choose between using foreign functions or type system. JavaScript functions might return not null values which have to be handled by the developer when designing these interfaces[12].
Call the GCD PureScript module from JavaScript, using FFI.
var Test = require('Test');
Test.gcd(15)(20);
Modules
[edit]Module keyword is used to refer to modules and all the code in PureScript is written in a module.
module A where
id x = x
When a module is imported (using import keyword) aliases are created for all values and types in it. Values, type constructors and data structures can be explicitly imported. Below is an example to import type constructors by passing a list of data constructors in parentheses.[13]
module B where
import A (runFoo, Foo(..), Bar(Bar))
Modules can be exported by providing a set of names in parentheses in the declaration similar to import.
Type Classes
[edit]Type classes is a powerful kind of abstraction provided by PureScript's type system. Following is the example of show, which is defined by type class in the Prelude module called Show.[14]
class Show a where
show :: a -> String
Below are some standard type classes defined in the Prelude and Standard libraries.
Eq: Eq type class is used to test two values for equality.
class Eq a where eq :: a -> a -> Boolean
Ord: The Ord type class is used to compare two values for types which support ordering.
data Ordering = LT | EQ | GT class (Eq a) <= Ord a where compare :: a -> a -> Ordering
Num: The Num type class is used to identify the types which support addition, subtraction, multiplication and division operations.
class (DivisionRing a) <= Num a
Do Notation
[edit]Do notation is a special syntax which is used to improve the readability of the code. It uses the keyword Do to introduce a block of code which uses do notation. It makes the structure of algorithm clearer. Following is the factors function written using do notation.[15]
factors :: Int -> Array (Array Int) factors n = filter (\xs -> product xs == n) $ do i <- 1 .. n j <- i .. n return [i, j]
Syntax
[edit]Functions and Records
[edit]Functions and records are the building blocks of PureScript.
Simple Types
[edit]PureScript defines seven built-in types: numbers, strings, booleans, integers, arrays, records, and functions which are defined in Prim[16] module (implicitly imported in every module). These can be seen in PSCi[17] by using the :type command. Following are a few examples.
$ pulp psci
> :type 1.0
Prim.Number
> :type "test"
Prim.String
> :type true
Prim.Boolean
> :type 1
Prim.Int
Unlike JavaScript, all the elements of PureScript array should be of same type.
> :type [1, 2, 3]
Prim.Array Prim.Int
> :type [true, false]
Prim.Array Prim.Boolean
> :type [1, false]
Cannot unify Prim.Int with Prim.Boolean.
Records
[edit]Records are PureScript's objects and the syntax of Record literals is same as that of JavaScript's Object literals[18].
> let author = { name: "Phil", interests: ["Functional Programming", "JavaScript"] }
> :type author
{ name :: Prim.String, interests :: Prim.Array Prim.String }
Dot is used to access the fields of records.
> author.interests
["Functional Programming","JavaScript"]
Functions
[edit]Functions can be defined inline ( using \ followed by space) or they can be defined at the top-level of file (by specifying arguments before equal sign).
> let
add :: Int -> Int -> Int
add = \x y -> x + y
or
add :: Int -> Int -> Int
add x y = x + y
Version History
[edit]Following data is based on the information provided in Github. [19]
Version v0.5.0 - Polymorph
[edit]Released on Apr 27, 2014.
Changes: Support for the block has been removed, multiple modules with same name are disallowed, Prelude module is automatically imported.
New features: Multi parameter type-classes, super-classes etc.
Enhancements: Row-type pretty printing was improved, escape check was removed.
Bug Fixes: Overlapping variables in type-class instances are removed, a bug related to in-lining was fixed.
Later versions v0.5.4, v0.5.5, v0.5.6, v0.5.6.1, v0.5.6.2, v0.5.6.3, v0.5.7, v0.5.7.1 have been released with no major changes.
Version v0.6.0 - Holoship
[edit]Released on Nov 8, 2014
Changes: Type class hierarchy was refactored, run time type checks has been removed etc.
New features: Newtypes are supported using newtype keyword, multi-line string literals are now supported.
Enhancements: Tail call optimization has been improved. Optimized the saturated calls to data constructors, optimized nested if blocks.
Bug Fixes: Module export related issues have been fixed, binding group calculation issues are fixed etc.
Versions v0.6.0.2, v0.6.1, v0.6.1.1, v0.6.1.2, v0.6.2, v0.6.3, v0.6.4, v0.6.4.1, v0.6.5, v0.6.6, v0.6.7, v0.6.7.1, v0.6.8, v0.6.9.3, v0.6.9.5 have been released later with few changes to v0.6.0
Version v0.7.0 - Meltdown
[edit]Released on Jun 29, 2015
Changes: Int and Number literals have been added, Prelude module is no longer imported automatically, FFI JavaScript cannot be provided inline.
Enhancements: Document improvements have been made, computations in Eff monad are now supported in PSCi.
Bug Fixes: Issues with pretty printer have been fixed.
See also
[edit]- JavaScript , dynamic , high level interpreted language
- CoffeeScript, a programming language that transcompiles to JavaScript
- GorillaScript, which offers additional type checking and syntactic sugar for common JS patterns such as inline callbacks
- LiveScript
- Amber Smalltalk
- Haskell,ML
- TypeScript, an open-source, strict superset of Javascript language from Microsoft
- RapydScript,[20] a translator with a more Pythonic syntax
References
[edit]- ^ https://hackage.haskell.org/package/purescript
- ^ http://www.purescript.org/
- ^ http://www.infoq.com/news/2014/09/purescript-haskell-javascript
- ^ http://www.pulpproject.org
- ^ https://github.com/purescript/purescript
- ^ https://www.npmjs.com/package/gulp-purescript
- ^ Freeman, Phil. PureScript by Example : Functional Programming for the Web.
{{cite book}}
:|format=
requires|url=
(help) - ^ http://www.purescript.org/learn/getting-started
- ^ http://curtis.io/purescript-for-web-development
- ^ https://en.wikipedia.org/wiki/Type_inference
- ^ https://leanpub.com/purescript/read#leanpub-auto-pattern-matching
- ^ https://gist.github.com/paf31/8e9177b20ee920480fbc#day-3---purescript-easy-ffi-and-purescript-oo-ffi
- ^ https://en.wikipedia.org/wiki/Modular_programming
- ^ https://leanpub.com/purescript/read#leanpub-auto-type-classes
- ^ https://leanpub.com/purescript/read#leanpub-auto-do-notation
- ^ https://github.com/requirejs/prim
- ^ https://github.com/purescript/purescript/wiki/PSCi
- ^ http://www.dyn-web.com/tutorials/object-literal
- ^ "PureScript – version history". Github.
- ^ "Rapydscript bitbucket repository". Atlassian Bitbucket. 3 Apr 2013. Retrieved 11 May 2014.
External links
[edit]Category:Dynamic programming languages Category:JavaScript programming language family Category:Software using the MIT license
Database Design
[edit]ResponseMap in essence, records who reviews whom. Schema of ResponseMap:
Field | Data type |
---|---|
id | int(11) |
reviewed_object_id | int(11) |
reviewer_id | int(11) |
reviewee_id | int(11) |
type | varchar(255) |
created_at | datetime |
updated_at | datetime |
caliberate_to | boolean |
reviewer_is_team | boolean |
The description of the fields of the database for ResponseMap:
1. id: The unique record identifier.
2. reviewed_object_id: The id of the object that is reviewed. Assignments or ReviewMaps could be reviewed.
3. reviewer_id: The reviewer can either be an “AssignmentTeam” or “AssignmentParticipant”, which is indicated by the field reviwer_is_team.
4. reviewee_id: The id “AssignmentTeam” who is getting the “Response” i.e., the one whose work is being reviewed.
5. type : Indicates the type of the ResponseMap.
6. created_at: The timestamp when the Response was created.
7. updated_at: The timestamp when the Response was updated.
8. calibrate_to: A field of boolean data type.
9. reviwer_is_team: If this field is ‘true’, the reviewer_id refers to the id of the “AssignmentTeam” to which the participant belongs. Else, it refers to the id of the “Participant” itself.
Key files to be modified
[edit]Models
[edit]- review_response_map.rb
- participant.rb
- response_map.rb
- response.rb
- assignment_team.rb
- assignment_participant.rb
- assignment.rb
Views
[edit]- review_mapping/_review_report.html.erb
Controllers
[edit]- review_mapping_controller.rb
- participants_controller.rb