SlideShare a Scribd company logo
Some Javascript
    useful perhaps
他山之石,可以攻玉
Stones from other hills may serve
to polish the jade of this one.

advice from others may help one
overcome one's shortcomings.
Ruby
           A Programmer’s Best Friend


This time we use Ruby to polish our
               Jade.
Integer.times


                    (10).times(function
10.times do |i| 
                    (i){

print i*10, " "
                    
print( i*10 + “ ”);
end
                    });
Number.step
1.step(10, 2) { |i| print i, " " }

Math::E.step(Math::PI, 0.2) { |f| print f, " " }



(1).step(function(i){ print(i); }, 10, 2);

Math.E.step(function(f){ print(f); },
Number Object

         modulo     abs
  ceil
             floor
round
             step
String.capitalise
"hello".capitalize #=> "Hello"
"HELLO".capitalize#=> "Hello"
"123ABC".capitalize#=>
"123abc"
"hello".capitalize() > "Hello"
"HELLO".capitalize() > "Hello" 
"123ABC".capitalize() > "123abc"
String.each_char

"hello".each_char {|c| print c, ' '} #=> "h e l l o
"




“hello”.each_char (function(c) {return c+” ”}) > “h
e l l o ”
String.insert
"abcd".insert(-3, 'X')   #=> 'abXcd'
"abcd".insert(-1, 'X')   #=> 'abcdX'
"abcd".insert(1, 'X')    #=> 'aXbcd'

"abcd".insert(-3, 'X')   > 'abXcd'
"abcd".insert(-1, 'X')   > 'abcdX'
"abcd".insert(1, 'X')    > 'aXbcd'
String.reverse

"Hello".reverse     #=> "olleh"




"Hello".reverse()   > "olleh"
String Object

        casecmp      insert
each_char
            capitalise
strip                 swapcase
          start_with
   end_with
                reverse
Array.each
[1,2,3].each {|i| print i+5 }   #=> 6 7 8

[1,2,3].each(function(i){ print( i+5) }) > 6 7 8
[1,2,3].reverse_each(function(i){ print( i+5) }) >
8 7 6
[{a:1,b:2},{a:2,b:3},{a:3,b:4}].each(function(item){
    print(item.a+item.b)
}) > 3 5 7
Array.map
a = [ "a", "b", "c", "d" ]
a.map {|x|x+"!" }       #=> ["a!", "b!", "c!", "d!"]
a                       #=> ["a", "b", "c", "d"]

var a = [ "a", "b", "c", "d" ];
a.map(function(i,item){return x+”!”}) > ["a!",
"b!", "c!", "d!"]
a                                           > [ "a",
"b", "c", "d" ]
Array.remove_if
          (Array.reject)
[ "a", "b", "c" ].reject {|x| x>="b" }   #=> ["a"]




[ "a", "b", "c" ].reject( function(i,x) {return x >=
"b" })
a = [ 4, 5, 6 ]
                 Array.zip
b = [ 7, 8, 9 ]
[1,2,3].zip(a, b)     #=> [[1, 4, 7], [2, 5, 8], [3,
6, 9]]
[1,2].zip(a,b)        #=> [[1, 4, 7], [2, 5, 8]]
a.zip([1,2],[8])      #=> [[4, 1, 8], [5, 2, nil],
var a = [ 4, 5, 6 ];
[6, nil, nil]]
var b = [ 7, 8, 9 ];
[1,2,3].zip(a, b)    > [[1, 4, 7], [2, 5, 8], [3, 6,
9]]
[1,2].zip(a,b)       > [[1, 4, 7], [2, 5, 8]]
a.zip([1,2],[8])      > [[4, 1, 8], [5, 2, null], [6,
null, null]]
Array.transpose
a = [[1,2], [3,4], [5,6]]
a.transpose                    #=> [[1, 3, 5], [2, 4,
6]]




var a = [[1,2], [3,4], [5,6]];
a.transpose()                   > [[1, 3, 5], [2, 4,
6]]
Array.rotate
a = [ "a", "b", "c", "d" ]
a.rotate             #=> ["b", "c", "d", "a"]
a                    #=> ["a", "b", "c", "d"]
a.rotate(2)          #=> ["c", "d", "a", "b"]
a.rotate(-3)         #=> ["b", "c", "d", "a"]

var a = [ "a", "b", "c", "d" ];
a.rotate()             > ["b", "c", "d", "a"]
a                      > ["a", "b", "c", "d"]
a.rotate(2)           > ["c", "d", "a", "b"]
a.rotate(-3)          > ["b", "c", "d", "a"]
Array.flatten
s = [ 1, 2, 3 ]         #=> [1, 2, 3]
t = [ 4, 5, 6, [7, 8] ] #=> [4, 5, 6, [7, 8]]
a = [ s, t, 9, 10 ]     #=> [[1, 2, 3], [4, 5, 6, [7,
8]], 9, 10]
a.flatten                #=> [1, 2, 3, 4, 5, 6, 7, 8,
9, 10]

a = [ 1, 2, [3, [4, 5] ] ]
a.flatten(1)              #=> [1, 2, 3, [4, 5]]
Array.sample

[1,2,3,4,5,6,7,8,9,10].sample()    #=> 7 (just
randomly selected)


[1,2,3,4,5,6,7,8,9,10].sample(3)   #=> 3, 9, 2
Array.shuffle


a = [ 1, 2, 3 ]         #=> [1, 2, 3]
a.shuffle               #=> [2, 3, 1]

var a = [ 1, 2, 3 ];      > [1, 2, 3]
a.shuffle()                > [2, 3, 1]
Array Object
                 is_empty
         push_all          size
      reverse_eachintersect clear
  each                     deduct
       map uniq        union
sample     at shuffle values_attranspose
   contains                        last
                      transpose
 keep_if     compact      select
       remove                    insert
  remove_if        reject      zip
       index      rotate      equals
                        take
   remove_at fetchtake_while
         flatten count
Examples

(1..10).map{|i| ("a".."z").to_a[rand 26] }.join             

new Array(10).map(function(i,item){

    return
"abcdefghijklmnopqrstuvwxyz"[(26*Math.random()).floor()]
;

}).join("");

(10).times(function(i){

    return
"abcdefghijklmnopqrstuvwxyz"[(26*Math.random()).floor()];

}).join("");
Examples

(1..10).map{|i| ("a".."z").to_a[rand 26] }.join             

new Array(10).map(function(i,item){

    return
"abcdefghijklmnopqrstuvwxyz"[(26*Math.random()).floor()]
;

}).join("");

(10).times(function(i){

    return
"abcdefghijklmnopqrstuvwxyz"[(26*Math.random()).floor()];

}).join("");
How I simply try
 these snippets
            nodejs




             irb
Thanks to
What if we can write
 and test like this
require '../src/com/ciphor/ruby/Array.js'
describe 'com.ciphor.ruby.Array', ->
    testArray = null
    beforeEach ->
      testArray = [1, 2, 3, 4, 5]
                                   Test for BDD -

                                Behaviour Driven
    afterEach ->
      testArray = null
                                   Developement

    it 'adds all elements in the given array into the self', ->
     testArray.push_all [6, 7, 8]
     expect(testArray.length).toEqual(8)
     expect(testArray).toContain(8)
+

...to be continued
Not


The End

More Related Content

What's hot (20)

ODP
The secrets of inverse brogramming
Richie Cotton
 
PPTX
Groovy
Zen Urban
 
PDF
Clojure functions midje
Jackson dos Santos Olveira
 
PPTX
Data Types and Processing in ES6
m0bz
 
DOCX
imager package in R and examples..
Dr. Volkan OBAN
 
PPTX
Oh Composable World!
Brian Lonsdorf
 
PDF
Ruby初級者向けレッスン 48回 ─── Array と Hash
higaki
 
PPTX
Introduction to R
Sander Kieft
 
PDF
python高级内存管理
rfyiamcool
 
PDF
A tour of Python
Aleksandar Veselinovic
 
PDF
Rのスコープとフレームと環境と
Takeshi Arabiki
 
PPTX
Ruby Language: Array, Hash and Iterators
Sarah Allen
 
PPTX
Ruby's Arrays and Hashes with examples
Niranjan Sarade
 
PPT
Gearmam, from the_worker's_perspective copy
Brian Aker
 
PDF
Cycle.js: Functional and Reactive
Eugene Zharkov
 
PDF
Gearman, from the worker's perspective
Brian Aker
 
PPTX
Millionways
Brian Lonsdorf
 
PDF
Elm: give it a try
Eugene Zharkov
 
PPTX
Groovy puzzlers по русски с Joker 2014
Baruch Sadogursky
 
ODT
Logic Equations Resolver J Script
Roman Agaev
 
The secrets of inverse brogramming
Richie Cotton
 
Groovy
Zen Urban
 
Clojure functions midje
Jackson dos Santos Olveira
 
Data Types and Processing in ES6
m0bz
 
imager package in R and examples..
Dr. Volkan OBAN
 
Oh Composable World!
Brian Lonsdorf
 
Ruby初級者向けレッスン 48回 ─── Array と Hash
higaki
 
Introduction to R
Sander Kieft
 
python高级内存管理
rfyiamcool
 
A tour of Python
Aleksandar Veselinovic
 
Rのスコープとフレームと環境と
Takeshi Arabiki
 
Ruby Language: Array, Hash and Iterators
Sarah Allen
 
Ruby's Arrays and Hashes with examples
Niranjan Sarade
 
Gearmam, from the_worker's_perspective copy
Brian Aker
 
Cycle.js: Functional and Reactive
Eugene Zharkov
 
Gearman, from the worker's perspective
Brian Aker
 
Millionways
Brian Lonsdorf
 
Elm: give it a try
Eugene Zharkov
 
Groovy puzzlers по русски с Joker 2014
Baruch Sadogursky
 
Logic Equations Resolver J Script
Roman Agaev
 

Viewers also liked (8)

PDF
Cansecwest_16_Dont_Trust_Your_Eye_Apple_Graphics_Is_Compromised
Liang Chen
 
PPTX
Moving Towards a Paperless Classroom
Macmillan Education
 
PPT
CatAnDogme - Creative Activities and dogme (by Jimmy Astley and Mauro Espindola)
Jimmy Astley
 
PPSX
Dogme elt demo
English Language Teaching
 
PPT
Dogme with young learners and beginners
Designed for You- Online English Classes
 
PDF
Dogme Workshop Materials
Ania Rolinska
 
PPTX
English world teacher training technology
Macmillan Education
 
PPT
Dogme ELT - a Pedagogy for Virtual Worlds
Howard Vickers
 
Cansecwest_16_Dont_Trust_Your_Eye_Apple_Graphics_Is_Compromised
Liang Chen
 
Moving Towards a Paperless Classroom
Macmillan Education
 
CatAnDogme - Creative Activities and dogme (by Jimmy Astley and Mauro Espindola)
Jimmy Astley
 
Dogme with young learners and beginners
Designed for You- Online English Classes
 
Dogme Workshop Materials
Ania Rolinska
 
English world teacher training technology
Macmillan Education
 
Dogme ELT - a Pedagogy for Virtual Worlds
Howard Vickers
 
Ad

Similar to Useful javascript (20)

PDF
Let’s Talk About Ruby
Ian Bishop
 
KEY
Haskellで学ぶ関数型言語
ikdysfm
 
PPTX
R programming language
Alberto Minetti
 
PDF
Refactoring to Macros with Clojure
Dmitry Buzdin
 
KEY
関数潮流(Function Tendency)
riue
 
PPTX
Super Advanced Python –act1
Ke Wei Louis
 
PDF
[1062BPY12001] Data analysis with R / week 2
Kevin Chun-Hsien Hsu
 
DOCX
Spark_Documentation_Template1
Nagavarunkumar Kolla
 
PDF
Extending Operators in Perl with Operator::Util
Nova Patch
 
PDF
Intoduction to numpy
Faraz Ahmed
 
PDF
An overview of Python 2.7
decoupled
 
PDF
Ruby Language - A quick tour
aztack
 
PDF
Python lecture 05
Tanwir Zaman
 
PDF
JavaScript Array Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Dr. Volkan OBAN
 
PDF
Palestra sobre Collections com Python
pugpe
 
PDF
PHP and MySQL Tips and tricks, DC 2007
Damien Seguy
 
PDF
An introduction to property-based testing
Vincent Pradeilles
 
PDF
Table of Useful R commands.
Dr. Volkan OBAN
 
Let’s Talk About Ruby
Ian Bishop
 
Haskellで学ぶ関数型言語
ikdysfm
 
R programming language
Alberto Minetti
 
Refactoring to Macros with Clojure
Dmitry Buzdin
 
関数潮流(Function Tendency)
riue
 
Super Advanced Python –act1
Ke Wei Louis
 
[1062BPY12001] Data analysis with R / week 2
Kevin Chun-Hsien Hsu
 
Spark_Documentation_Template1
Nagavarunkumar Kolla
 
Extending Operators in Perl with Operator::Util
Nova Patch
 
Intoduction to numpy
Faraz Ahmed
 
An overview of Python 2.7
decoupled
 
Ruby Language - A quick tour
aztack
 
Python lecture 05
Tanwir Zaman
 
JavaScript Array Interview Questions PDF By ScholarHat
Scholarhat
 
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Dr. Volkan OBAN
 
Palestra sobre Collections com Python
pugpe
 
PHP and MySQL Tips and tricks, DC 2007
Damien Seguy
 
An introduction to property-based testing
Vincent Pradeilles
 
Table of Useful R commands.
Dr. Volkan OBAN
 
Ad

Recently uploaded (20)

PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 

Useful javascript

  • 1. Some Javascript useful perhaps
  • 2. 他山之石,可以攻玉 Stones from other hills may serve to polish the jade of this one. advice from others may help one overcome one's shortcomings.
  • 3. Ruby A Programmer’s Best Friend This time we use Ruby to polish our Jade.
  • 4. Integer.times (10).times(function 10.times do |i| (i){ print i*10, " " print( i*10 + “ ”); end });
  • 5. Number.step 1.step(10, 2) { |i| print i, " " } Math::E.step(Math::PI, 0.2) { |f| print f, " " } (1).step(function(i){ print(i); }, 10, 2); Math.E.step(function(f){ print(f); },
  • 6. Number Object modulo abs ceil floor round step
  • 7. String.capitalise "hello".capitalize #=> "Hello" "HELLO".capitalize#=> "Hello" "123ABC".capitalize#=> "123abc" "hello".capitalize() > "Hello" "HELLO".capitalize() > "Hello" "123ABC".capitalize() > "123abc"
  • 8. String.each_char "hello".each_char {|c| print c, ' '} #=> "h e l l o " “hello”.each_char (function(c) {return c+” ”}) > “h e l l o ”
  • 9. String.insert "abcd".insert(-3, 'X') #=> 'abXcd' "abcd".insert(-1, 'X') #=> 'abcdX' "abcd".insert(1, 'X') #=> 'aXbcd' "abcd".insert(-3, 'X') > 'abXcd' "abcd".insert(-1, 'X') > 'abcdX' "abcd".insert(1, 'X') > 'aXbcd'
  • 10. String.reverse "Hello".reverse #=> "olleh" "Hello".reverse() > "olleh"
  • 11. String Object casecmp insert each_char capitalise strip swapcase start_with end_with reverse
  • 12. Array.each [1,2,3].each {|i| print i+5 } #=> 6 7 8 [1,2,3].each(function(i){ print( i+5) }) > 6 7 8 [1,2,3].reverse_each(function(i){ print( i+5) }) > 8 7 6 [{a:1,b:2},{a:2,b:3},{a:3,b:4}].each(function(item){ print(item.a+item.b) }) > 3 5 7
  • 13. Array.map a = [ "a", "b", "c", "d" ] a.map {|x|x+"!" } #=> ["a!", "b!", "c!", "d!"] a #=> ["a", "b", "c", "d"] var a = [ "a", "b", "c", "d" ]; a.map(function(i,item){return x+”!”}) > ["a!", "b!", "c!", "d!"] a > [ "a", "b", "c", "d" ]
  • 14. Array.remove_if (Array.reject) [ "a", "b", "c" ].reject {|x| x>="b" } #=> ["a"] [ "a", "b", "c" ].reject( function(i,x) {return x >= "b" })
  • 15. a = [ 4, 5, 6 ] Array.zip b = [ 7, 8, 9 ] [1,2,3].zip(a, b) #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]] [1,2].zip(a,b) #=> [[1, 4, 7], [2, 5, 8]] a.zip([1,2],[8]) #=> [[4, 1, 8], [5, 2, nil], var a = [ 4, 5, 6 ]; [6, nil, nil]] var b = [ 7, 8, 9 ]; [1,2,3].zip(a, b) > [[1, 4, 7], [2, 5, 8], [3, 6, 9]] [1,2].zip(a,b) > [[1, 4, 7], [2, 5, 8]] a.zip([1,2],[8]) > [[4, 1, 8], [5, 2, null], [6, null, null]]
  • 16. Array.transpose a = [[1,2], [3,4], [5,6]] a.transpose #=> [[1, 3, 5], [2, 4, 6]] var a = [[1,2], [3,4], [5,6]]; a.transpose() > [[1, 3, 5], [2, 4, 6]]
  • 17. Array.rotate a = [ "a", "b", "c", "d" ] a.rotate #=> ["b", "c", "d", "a"] a #=> ["a", "b", "c", "d"] a.rotate(2) #=> ["c", "d", "a", "b"] a.rotate(-3) #=> ["b", "c", "d", "a"] var a = [ "a", "b", "c", "d" ]; a.rotate() > ["b", "c", "d", "a"] a > ["a", "b", "c", "d"] a.rotate(2) > ["c", "d", "a", "b"] a.rotate(-3) > ["b", "c", "d", "a"]
  • 18. Array.flatten s = [ 1, 2, 3 ] #=> [1, 2, 3] t = [ 4, 5, 6, [7, 8] ] #=> [4, 5, 6, [7, 8]] a = [ s, t, 9, 10 ] #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10] a.flatten #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a = [ 1, 2, [3, [4, 5] ] ] a.flatten(1) #=> [1, 2, 3, [4, 5]]
  • 19. Array.sample [1,2,3,4,5,6,7,8,9,10].sample() #=> 7 (just randomly selected) [1,2,3,4,5,6,7,8,9,10].sample(3) #=> 3, 9, 2
  • 20. Array.shuffle a = [ 1, 2, 3 ] #=> [1, 2, 3] a.shuffle #=> [2, 3, 1] var a = [ 1, 2, 3 ]; > [1, 2, 3] a.shuffle() > [2, 3, 1]
  • 21. Array Object is_empty push_all size reverse_eachintersect clear each deduct map uniq union sample at shuffle values_attranspose contains last transpose keep_if compact select remove insert remove_if reject zip index rotate equals take remove_at fetchtake_while flatten count
  • 22. Examples (1..10).map{|i| ("a".."z").to_a[rand 26] }.join new Array(10).map(function(i,item){ return "abcdefghijklmnopqrstuvwxyz"[(26*Math.random()).floor()] ; }).join(""); (10).times(function(i){ return "abcdefghijklmnopqrstuvwxyz"[(26*Math.random()).floor()]; }).join("");
  • 23. Examples (1..10).map{|i| ("a".."z").to_a[rand 26] }.join new Array(10).map(function(i,item){ return "abcdefghijklmnopqrstuvwxyz"[(26*Math.random()).floor()] ; }).join(""); (10).times(function(i){ return "abcdefghijklmnopqrstuvwxyz"[(26*Math.random()).floor()]; }).join("");
  • 24. How I simply try these snippets nodejs irb
  • 26. What if we can write and test like this require '../src/com/ciphor/ruby/Array.js' describe 'com.ciphor.ruby.Array', -> testArray = null beforeEach -> testArray = [1, 2, 3, 4, 5] Test for BDD - Behaviour Driven afterEach -> testArray = null Developement it 'adds all elements in the given array into the self', -> testArray.push_all [6, 7, 8] expect(testArray.length).toEqual(8) expect(testArray).toContain(8)