• Skip to main content
  • Skip to footer

InRhythm

Your partners in accelerated digital transformation

  • Who We Are
  • Our Work
  • Practices & Products
  • Learning & Growth
  • Culture & Careers
  • Blog
  • Contact Us

JavaScript

Jul 11 2016

Code Challenge Accepted (And Answered)

One of engineers challenged our entire team with the following task:

 

Code challenge (in js, obvi).
Write a one-liner that outputs (int) 10 using only the following symbols: “+”, “[” and “]”
Oh, and you have to explain your answer.
Extra credit: Same restrictions, output (string) 20.

Now if you want to play around a little bit and try and figure out the answer – feel free! Make sure to drop your answer or logic in the comments below. Let us know if you want to see more challenges on the blog as well.

 

But for those of you who want to dive right into it – here is our answer explained!

 

What happens when we perform a math operation on an empty array?

 

+[] // 0

Int 0. Int-eresting, it looks like javascript automatically type casts our array into an int.

 

Now, using some basic cs principles we can have some fun. Let’s try outputting int 1. We know that we can get 0 from +[] so let’s try to increment it

 

++[] // Invalid left-hand side expression

Well… that sucks. However, we’ve got another trick up our sleeve. We can shove our outputted 0 into an array, access index[0] and then increment that.

 

Huh?

Just watch, you’ll get it.

 

[+[]]
That’s our array[0], now let’s get to that precious 0 it’s keeping safe for us.

 

[+[]][0]
Bingo, but we can do better. We know +[] is zero, so we can access the 0 index using +[] instead

 

[+[]][+[]]
Bang, we’ve got our 0 again. But we’ve already had a 0, what’s the difference? I’ll tell you. Nothing… except that this 0 won’t throw an invalid left handed expression error when we try to increment it.

 

++[+[]][+[]]
And there’s our int 1.

 

Let’s try and get a string 20. Seems pretty easy. We can take our newly created int 1 and increment that to a 2 then just add a string 0. But how do we get a string 0. Simple. We just add two arrays together. That makes sense, right?

 

[]+[] // empty string

Now, what happens when you add a string and an int? It gets cast as a string.

 

[++[[++[+[]][+[]]+[]]][+[]]]+[+[]] === "20" // true
Have fun wrapping your head around that one.

 

barney-more-challenges

Written by inrhythmAdmin · Categorized: Software Engineering · Tagged: challenge, code, development, engineering, growth, JavaScript, puzzle, software

Mar 23 2016

Why Functional Programming needs to be at the top of your list in 2016

Every month there seems to be a new set of tools that the software community is raving about. Whether it’s a new framework, build tool, or obscure language, there always some new fad to learn.

When first introduced to the concept of functional programming, I assumed it would fall somewhere in that “fad” category. A topic I would read a short article about and then move on.

But, with just a few implementations of its concepts many of the issues that had plagued me for years seemed to be less of a concern. When I stuck with functional concepts, I no longer had to deal with monolithic objects that seemed to only grow in size and monstrosity.

With pure functions, unforeseen behavior or spooky side effects, from a distance, were less of a concern. It was as if a light had gone off and I could finally see clearly how much faster and stable I could build programs. If only the tools, community, and frameworks reflected this new paradigm.

Fortunately in the world of programming, especially front end development, we’re witnessing a subtle shift from Imperative to Declarative.

Before we dive into Functional and Object Oriented, it’s important to appreciate the bedrock of modern programming or Imperative and Declarative. For over thirty years we’ve approached programming from an Imperative style. We give a list of commands and tell the computer how we want to accomplish an action. Primarily based on the idea that you are constantly changing something in memory, mutability.

Imperative Thinking

Imperative is built around the idea that while you’re viewing or changing something in memory the rest of the world freezes. One can easily see how in a world filled with concurrency that could be a problem. By now I’m sure you’re wondering why would we ever revert to an Imperative style, but if you look at its predecessor with Procedural it becomes evident.

We’ll save Procedural for another piece, but the real question is – if Object Oriented is so touted why is it imperative?

Perhaps the most concerning idea that Object Oriented promotes is mutating state. It’s so frequent in Object Oriented languages that we simply take it as a necessity of programming. But state makes scaling apps extremely difficult as we have less certainty on values.

[snippet id=”86″]

Take this small example of a few lines of code. Because of mutating state and mutable objects we lose certainty of the value. By the time we console.log the value, it could be anything.

This only grows more concerning as we add other modules and objects. Object Oriented reliance on mutating state is concerning, to say the least.

Take that concept along with inheritance which promotes the idea of unforeseen interdependence – we frequently have code that is filled with uncertainty and hard to reason about.

The excuse that most apologist make for Object Oriented is that its objects serve as abstractions, or interfaces. But its more than just abstractions, OO promotes convoluted code.

The problem with object-oriented languages is they’ve got all this implicit environment that they carry around with them. You wanted a banana but what you got was a gorilla holding the banana and the entire jungle. — Joe Armstrong

The list goes on on from encapsulation to concurrency issues to polymorphism we could spend a week discussing the problems and only scratch the surface. But I’d rather focus on a “new” way to program. One grounded on the idea that rather telling the computer how to do something, emphasizing what we want.

The important idea to take away is that Imperative is based on a false premise. It assumes that you are the only one trying to interact with a value and that mutating data is a good approach to scaling an app.

The Way Forward

This idea of emphasizing what we want instead of how we want it done is the cornerstone of Declarative Programming.

At it’s best functional programming is declarative programming. It emphasizes small functions that composed together to give the computer what output we want from a certain input value. Think of it as functions mapping values.

[snippet id=”87″]

If you notice in this first example we’re telling the computer how we want to accomplish a task. Whereas in the second example, we emphasize the verbs or action we want to do. But before we dive further into the future lets take a look back.

Looking Back

To truly understand the paradigm shift that functional programming presents we have to think back to just what was capable seven to ten years ago. From a cutting edge company, we could expect a site to look and feel something like this.

QJd1tNY

We might laugh now, as we should, but the reason it lacks so much is that we simply didn’t have the necessary tools to create our vision.

What we create is directly proportional to the ability of our tools. If in the next ten to twenty years we want to accomplish the goals of virtual reality, machine learning and even things like space travel, the tools we use must become far more efficient and reliable.

We’re already starting to see more functional aspects in OO design with promises, immutable objects and libraries like lodash and underscore. But we’re just at the beginning of a shift and now is the time to become more familiar and comfortable with the topic.

Functional programming is often overlooked because it appears complex, theoretical and dare I say mathematical. Yet in fact its completely approachable. I’ve put together a guide detailing how one can become quickly fluent in functional programming. All you need is a basic understanding of front-end development and JavaScript.

But before you even dive into the guide, I’d suggest to get started thinking functionally by taking a small piece of code and answering two questions about it.

  1. What is the lifecycle of this object?
  2. What other variables or objects outside of this object are dependent upon it?

—

This is the first piece in a series The Functional Programmer’s Starter Kit and was written by contributing writer – Garreth Dottin.

Written by inrhythmAdmin · Categorized: Software Engineering · Tagged: functional programming, guide, JavaScript, software, tools

Mar 17 2016

March Meetup Video – Converting a Project from ES5 to ES2015

For our March Meetup we all gathered at IRHQ with Glenn Hinks to discuss switching a project over from ES5 to ES2015. Glenn has been writing software since 1981 and is a full time node developer working at a live video sports streaming startup. We are excited to have exclusive full video footage for you right here!

In this video you will learn how to begin the process of switching your own projects, why it’s beneficial to do so, recommended tools to use, how to get started, and even what pain points you might meet along the way.

Loved this video? Check out the rest of our Meetup talks on our YouTube channel and come to our next event.

Written by inrhythmAdmin · Categorized: Events, Software Engineering · Tagged: JavaScript, Meetup, tutorial, video

Feb 22 2016

February Meetup Roundup – Reactive Layout for User Interfaces

Let’s take a moment to think about how we interact with interfaces. I am willing to bet that about half of you think “point and click” while the other half considers “point and move”. At our most recent Meetup, David Valdman covered the topic of Reactive Layout for User Interfaces.

Sounds cool, but what does that mean? David started by giving us a little background on the difference between Object Oriented Programming and Reactive Programming. Let’s start with a question:

If a tree falls in a forest but no one is there to hear it, does it still make a sound? Has it even fallen?

We didn’t have any philosophy members to really help us dive into this conundrum. However, this is a great metaphor for Object Oriented and Functional programming. Still confused? It’s all revealed in the video above, including a more in-depth explanation of what the relationship is between reactive programming, layouts, and animation. David also dives into the JavaScript library, Samsara.js, which he recently published for animating layout.“It provides a language for positioning, orienting and sizing DOM elements and coordinating the animation of these properties over time. To do so, it borrows from the principles of reactive programming (samsarajs.org).

The talk was fascinating and we have full video coverage above for you to enjoy. We are excited to move into this new “point and move” territory together with you.

Written by inrhythmAdmin · Categorized: Events, Software Engineering · Tagged: JavaScript, Meetup, reactive programming, tutorial, video

Jul 21 2015

Oh What a Night – Node.js Meetup Recap

Last week our Engineers gathered together for an instruction on Node.js with Sandro Pasquali, author of Mastering Node.js. It was a great night filled with study, hands-on coding and creative exploration. All our participants walked away having built a customer service platform upon an SMS messaging layer; a full application in about 120 minutes. Beyond Node, we made use of websockets, Gulp, React, ES6 and more. We’re excited for our next Meetup which will be focused within the UX arena. See you all there!

 

Written by inrhythmAdmin · Categorized: Events · Tagged: education, ES6, JavaScript, Meetup, Node.js, React

  • « Go to Previous Page
  • Go to page 1
  • Go to page 2
  • Go to page 3
  • Go to page 4
  • Go to Next Page »

Footer

Interested in learning more?
Connect with Us
InRhythm

140 Broadway
Suite 2270
New York, NY 10005

1 800 683 7813
get@inrhythm.com

Copyright © 2022 · InRhythm on Genesis Framework · WordPress · Log in

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT