DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Fun javascript feature

Fun javascript feature

Swizec Teller user avatar by
Swizec Teller
·
Dec. 09, 11 · Web Dev Zone · Interview
Like (0)
Save
Tweet
3.17K Views

Join the DZone community and get the full member experience.

Join For Free

not only was my nondeterministic turing machine implementation way too long at 20 lines, it was also wrong. shortly after @dionyziz reported a bug and it took me until last night to get around to fixing it.

class np

image by henry work via flickr

the problem was that when i was passing tapes for each possible step into the next iteration of the algorithm the good old “everything is a reference” javascript feature bit me in the arse. because of this all the tapes looked exactly like the tape belonging to the last state inspected and so when you moved the instructions around a bit you could make my implementation fail without actually changing the actual turing machine.

fixing it was a simple matter of strategically using underscore’s clone function to make sure ever inspected state is acting on its own version of the tape.

and since the fix was so simple and i didn’t feel like studying the subject that has been the bane of my existence for the past two years, i decided to try codegolfing the algorithm a bit. doing so i accidentally came upon what is possibly javascript’s funnest feature.

var δ=function(s,s,e){var π=_.keys,θ={},k,i,λ,μ,ι,σ=_.size,β,ψ;
if(π(s).indexof(e)>=0)return!0
for(k in s){i=s[k][0],λ=s[k][1],ψ=s[k][λ[i]]||s[k].b;for(ι=0;ψ,ι<σ(ψ);){
μ=ψ[ι++],β=_.clone(λ),β.splice(i,1,μ.w),θ[μ.n]=[i+μ.m,β]}}return σ(θ)?δ(s,θ,e):!!0};

glorious 252 characters of nondeterministic turing machine right there! i’d say 252 bytes, but it’s not, look at all those shiny unicode characters!

turns out even though javascript legally only allows numbers, letters and the $ and _ signs for variable names … using almost any imaginable unicode character is also legal. perhaps slightly difficult to type, but definitely a useful feature for making more expressive code.

especially when codegolfing or implementing physics or maths.

not sure i’ll ever be using this again though … oh and here’s a slightly more readable version:

var δ = function(s,s,e){
    var π=_.keys,θ={},k,i,λ,μ,ι,σ=_.size,β,ψ;
    if(π(s).indexof(e)>=0) return!0
    for(k in s){
        i=s[k][0],
        λ=s[k][1],
        ψ=s[k][λ[i]]||s[k].b;
 
        for(ι=0; ψ,ι<σ(ψ); ){
            μ=ψ[ι++];
            β=_.clone(λ);
            β.splice(i,1,μ.w);
            θ[μ.n]=[i+μ.m,β];
        }
    }
    return σ(θ)?δ(s,θ,e):!!0;
};

before you ask, machine instructions look like this and you can find a script for running all of this over at my github .

from http://swizec.com/blog/fun-javascript-feature/swizec/3141

JavaScript Turing machine

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Is DataOps the Future of the Modern Data Stack?
  • Is Your Code DRY or WET?
  • 7 Ways to Capture Java Heap Dumps
  • Practice on Pushing Messages to Devices of Different Manufacturers

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo