14. Misc topics
Today's topics
Human-powered programming
Callback programming
Loop programming
Human-powered programming
Human as programming resource
Human power is the ultimate computing resource
Some problems are solved only by humans
Which restaurant is the best?
Interesting books?
Using human power for programming
Pair programming
Programming collective intelligence
Merging human power and computing power
Web = platform for using human power
Super parallel computingn by humans
Ultimate computing resource
Implicit human programming
Want to calculate complicated things
⇒ Create a service for gathering info
⇒ Get info from gathered data
Examples of human-powered calculation
Find a good restaurant
Find a influential person
Calculate the beauty of a lady
...
Calculation without notice
People often do something without notice
Boast about something
Correct something
Calculation with joy
Wikipedia
Hotornot
Kotonoha
reCAPTCHA
Wikipedia
World-biggest human calculation project
Calculate how beautiful a person is
http://gyazo.com/2dc0aa135251d5ad4f74c1b9a610c2f2.png
Simple inquiry
http://gyazo.com/d9f0658089b8e185d3005eca5d9f90b4.png
CAPTCHA
Prove that the user is a human
http://gyazo.com/d200e187b92f98e4d639ed8d23d5203f.png
reCAPTCHA
Authentication and character recognition
Not really interesting, though
http://gyazo.com/7c26209860f904a1feff6ca10d0aa073.png
Fixing something without notice
Correcting something if inappropriate
Picking up trash
Power of Low Standards
http://gyazo.com/c9147cba2b040dd25331909d064c0fa5.png
Location registration
Feel uneasy when registration is wrong
http://gyazo.com/bb7642f350fff42abb9f7a61bf31657b.png
After correction
Correct location registered
http://gyazo.com/61289b265ca621d8f77db568c2110c81.png
Feeling uneasy if text has errors
Want to fix it
Using curiocity
Mutual proofreading
http://gyazo.com/aa5264fa4e48875c793a1cf9b26e0379.png
Diary proofreading
http://gyazo.com/97901c841fc693ae6d51ce1bac74d54b.png
Crowdsourcing
Ask somebody on the net to work
Crowdsourcing service by Amazon
Now available in Japan (2017/6-)
https://gyazo.com/279e5f7883dafdcfcfb3933ac01c296d.png
Crowd-based word processor
Menu entry like "shorten this paragraph"
https://gyazo.com/8d2f6dcb50cde919e403735b9e92e20e.png
Problem of crowdsourcing
Too little money gained
Got only $1.8 for 1-hour tough work
$5 average?
New kind of exploitation?
Programming humans
Recipe for preparing pasta
Boil a pan of water
Put some salt
Wait until water is hot enough
while(not not){ sleep(10) }
Put pasta into boild water
Wait 10 minutes
sleep(600)
Programs and recipes
Many people hate programming
Everyone can understand recipes
Recipe = program for human
BabaScript
Explicit human programming proposed by Baba-san
Treat computers and humans the same way
Humans work as computing resources
List of instructions
Sample instruction
http://gyazo.com/f7cb9135ecf7e4bf40aaa43d5e4c724f.png
Script for creating pasta
Instructions for humans are written in JavaScript
http://gyazo.com/af526e4b31557f9b331679fe46eff8fe.png
Application on smartphone
http://gyazo.com/e6e0d85e04cf4f6e6ed94cccd49d3de0.png
Receiving a request
Select one and reply
http://gyazo.com/58b715a4ed9d9306d78fe34930bc047c.png
Recommendation, etc.
Implicit programming
Use everyday activities for computation
Not aware of computing something
Using Google
Finding important pages
Use man-made links
Editing links = intelligent computation
Page rank
Invented by Larry Page (Google)
Use link information for page ranking
Linked pages considered important
Human power over natural language processing
Larry Page
https://gyazo.com/5a7ea84315f9ec3569afb46f070aa3d3
https://gyazo.com/43e0c52c1f5ab35dab98ed2b2ba40d90
X=Z
Y=X/2
Z=X/2+Y
⇒ X : Y : Z = 0.4 : 0.2 : 0.4
Finding important files
Backup only important files
Calculation of importance
What are "important files"?
× Movies
× System files
× MP3 files
○ Personal photos
○ Idea memos
Difficult to tell from attributes
Important files
Not shared by other people
Personal photos
Idea memos
Calculating file importance
Calculate hash values and share
If the value is unique, the file may be important
Visualization of file sharing status
https:///gyazo.com/5124de74689ff55d53ffb3da92c1c636.png
Visualization of duplicate files
https://gyazo.com/4418725289623822fa490081d48ec670.png
HashInfo.com
Register filenames and hash values
Useful if many people share information
Proving the existance of information
Using a notary
Using a certification authority (CA) on the net
Money and time required
Proof of existence by registration
Calculate hash value of a file
e.g. 0a1b2c3d
Generate a URL which contain the hash
e.g. http://example.com/0a1b2c3d
Register the URL on social bookmarking services and SNS
del.ico.us
Facebook
Social proof of existence
F = target file
Want to prove that F existed at a certain time
Use auth command
F: atarget file
H: hash vaelu of F のハッシュ (e.g. 0a1b2c3d)
Copy F to S
Register S to U
Register U to various bookmark services and SNS
Social proof of existence
U is found everytwhere
Timestamp of U registered
Impossible to create fake U
U cannot be generated without F
⇒ Existence of F proved
Data mining from shared data
Programming collective intelligence
http://gyazo.com/3cfa17df5e77ccc67b7bd5f8f6a8279f.png
Programming collective intelligence
Recommendation systems
Clustering
Decision tree
SVM
Recommendation systems
Collaborative Filtering
e.g. Firefly
Firefly
Register musics you like
Recommend musics based on the data
Registration takes time
Last.fm
Preferences accumulated
No need for manual registration
Extract information from hondana.org data
Invoke methods in Japanese
Various calculation
Visualization by Hayashi's quantification method
---------------------------------------------
Callback programming
Asynchronous programming
Became popular on JavaScript
Run heavy process later
Normal programming
code:funcs.js
func1();
func2();
func3();
Execution stops if func2() is heavy and slow
Browser stalls
Solutions
Parallel programming
Callback programming
Example of callback programming
code:callback.js
func1();
func2(arg1,arg2,function(){
// heavy or slow operations
// e.g. communication with slow server
console.log("end")
});
func3();
func2() finishes immediately
Calculation in background
"end" printed after calculation
Advantages of callback programming
Users should not wait termination
Pseudo parallel programming
Disadvantages of callback programming
Difficult to understand
Process ordering not guaranteed
Guaranteed execution order
Callback nesting
Difficult to understand deep nesting
"Callback hell"
code:async1.js
setTimeout(function(){
console.log('First');
setTimeout(function(){
console.log('Second');
setTimeout(function(){
console.log('Third');
},100);
},500);
},1000);
Guaranteed execution order
Use special libraries
async.js
Parallel/sequential execution
Async library
code:async2.js
var async = require('async');
async.series([
function (callback) {
console.log('series 1');
setTimeout(function () {
console.log('series 1 done.');
callback(null, 1);
}, 500);
},
function (callback) {
console.log('series 2');
setTimeout(function () {
console.log('series 2 done.');
callback(null, 2);
}, 300);
},
function (callback) {
console.log('series 3');
setTimeout(function () {
console.log('series 3 done.');
callback(null, 3);
}, 100);
}
], function (err, results) {
if (err) { throw err; }
console.log('series all done. ' + results);
});
Problems of using libraries
Extra specification
Difficult to handle errors
Explicit parallelism might be better
Promise and async-await
New feature of JavaScript
Handle asynchronus processing easily
Promise
Abstraction of asynchronous operation
Conventional callback
code:callback.js
getAsync("fileA.txt", function(error, result){
if(error){
// do error handling
throw error;
}
// process the result
});
code:promise.js
var promise = getAsyncPromise("fileA.txt");
promise.then(function(result){
// process the result
}).catch(function(error){
// error handling
});
Async-await
Use Promise for synchronous operations
code:async.js
function openFile(url) {
const p = new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.addEventListener('load', (e) => resolve(xhr));
xhr.send();
});
return p;
}
async function loadAllFiles() {
const xhr1 = await openFile('foo.txt');
const xhr2 = await openFile('bar.txt');
const xhr3 = await openFile('baz.txt');
console.log('done!');
}
loadAllFiles();
Future of parallel programming
Semaphores, monitors are not convenient
Callback programming is difficult
History of loop processing
Old loop programs
Many GOTO statements
"spaghetti programming"
http://gyazo.com/ba6a0d78000d76428b935d3a1d63c4af.png
Criticism on GOTO statement
Proposed 'structured programing'
Structured programming
Restrict program structure
while, if, etc.
GOTO considered harmful
NS chart
http://gyazo.com/82bd29da9867ec20105a3d7de374441d.png
PAD
http://gyazo.com/127b8cf6de5ffa9915efb63ba2a95520.png
http://gyazo.com/7e50d657a9eade1a7af627a4315fd9c0.png
HCP chart
http://gyazo.com/b220896bd7c83977b8a3afa3bd2fdc37.png
Invention of for statement
for(init;cond;change)
Intruduced in C
Everything related to loop is in one line
Less bugs
XXcharts became extinct
e.g. while statement
code:while.txt
init
while(condition){
(Do something)
change values
}
Removing loops
map function
Apply a function to all elements
No loop statment
map function
code:map.rb
map.rb
puts s
}
Conclusions
Various programming pradigms
Enjoy programming anywhere!