Friday, January 27, 2017

Android Sensors in Depth: Proximity and Gyroscope

Erlang and Elixir, Part 4: Control Flow

If, else, and logical operators are probably the most common similarity between the popular languages, and unsurprisingly Elixir has them, too. The if, case, and cond macros are provided for giving us control flow structure. 

For pattern matching, we mentioned before that using case provides matches against any pattern iteratively:

Worth noticing here is the _ case, which is essentially a default catch-all case. The case can be also used with an atom or any variable type like so.

Guard Clause Expressions

Elixir provides many operators to check in our expression as a guard against catching the wrong data. By default, the following are supported:

  • comparison operators (==!====!==>>=<<=)
  • boolean operators (andornot)
  • arithmetic operations (+-*/)
  • arithmetic unary operators (+-)
  • the binary concatenation operator <>
  • the in operator as long as the right side is a range or a list
  • all the following type check functions:
    • is_atom/1
    • is_binary/1
    • is_bitstring/1
    • is_boolean/1
    • is_float/1
    • is_function/1
    • is_function/2
    • is_integer/1
    • is_list/1
    • is_map/1
    • is_nil/1
    • is_number/1
    • is_pid/1
    • is_port/1
    • is_reference/1
    • is_tuple/1
  • plus these functions:
    • abs(number)
    • binary_part(binary, start, length)
    • bit_size(bitstring)
    • byte_size(bitstring)
    • div(integer, integer)
    • elem(tuple, n)
    • hd(list)
    • length(list)
    • map_size(map)
    • node()
    • node(pid | ref | port)
    • rem(integer, integer)
    • round(number)
    • self()
    • tl(list)
    • trunc(number)
    • tuple_size(tuple)

We can use them like so:

Additionally, an anonymous function can have multiple guards. For example, to calculate a pivot point from a financial market data using the high, low, and close values, we can do this:

Here, if the high value is less than low, the pivot anonymous function will return an Error message. This one-line fashion of writing guards is phenomenally powerful.

Cond

Like a switch statement, Elixir's cond is where we can perform a string of if else like blocks and execute on the match.

Inside a cond block, everything is evaluating to true except nil or false. That's all numerical values and strings included. 

For example, we can check the current status for various possibilities with a cond block:

When the value of status is set to a string, it will evaluate; otherwise, an error message will be output by default via the last true case.

If and Unless

Elixir also provides us with unless, which is a default part of the if else block and can be demonstrated as so:

When the if case evaluates as true, unless will allow you to have access to the opposite. As demonstrated, we can catch the other side of the conditional this way.

In a real-life example, this can be used for displaying conditional information during a conditional check, such as a reminder or hint. 

Do/End Blocks

Throughout these examples, we have seen the usage of the do and end macros. Let's have a closer look at them now before we conclude this section:

The comma after true is for Elixir's regular syntax, where each argument is comma-separated. This syntax uses keyword lists, and when used in conjunction with the else macro it will look like this:

To make this nicer for development, we can use the do/end block, which does not require the comma for a direct approach:

Conclusion

Elixir has the familiar control flow structures if/else and case, and it also has unless and the do/end block.

We can achieve any logic via manipulation of control flow with these structures. When you use them in conjunction with macros, anonymous functions, and modules, you have a great supply of options to get the work done. 

For continued reading on the topic, the manual provides more insights into some of the further caveats of using the do/end block, such as function scope limitations. 


by Tom Whitbread via Envato Tuts+ Code

Content Marketing Success: Why Answering Questions Sells

Do you want more sales? Is content marketing a part of your strategy? To explore how to create content that sells, I interview Marcus Sheridan. More About This Show The Social Media Marketing podcast is an on-demand talk radio show from Social Media Examiner. It’s designed to help busy marketers and business owners discover what [...]

This post Content Marketing Success: Why Answering Questions Sells first appeared on .
- Your Guide to the Social Media Jungle


by Michael Stelzner via

How to Share 360 Panorama Photos with WebVR and A-Frame

Cannabis

Dive into the heart of weed traffic in our last project for the new french TV show CANNABIS
by via Awwwards - Sites of the day

Thursday, January 26, 2017

This Facebook Ad Targeting Guide Will Help You Save Money With Better Targeting (infographic)

This Facebook ad targeting guide created by the Facebook advertising agency AdvertiseMint gives advertisers a quick and easy glimpse of Facebook’s 850 ad targeting options—yes, that 850 ways to target high-interest individuals likely to convert. The infographic-guide is segmented into the main...

[ This is a content summary only. Visit our website http://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Web Desk via Digital Information World

This week's JavaScript news, issue 319

This week's JavaScript newsRead this e-mail on the Web
JavaScript Weekly
Issue 319 — January 26, 2017
Dr. Axel demonstrates how a proposed ‘spec mode’ for Babel will help you prepare for a world of native ES modules.
Dr. Axel Rauschmayer

How to significantly change 30,000 lines of client-side JavaScript without breaking things? This team tried both Flow and TypeScript and brought some minimal typing into play.
Nicholas Kariniemi

Ionic 2.0.0 comes with new cross-platform components with 100% Material Design support, a 'native feature' plugin system, and enhanced performance.
Official Ionic Blog

npm, Inc.
npm’s the obvious choice for managing JavaScript dependencies, but when Ellie Mae built a web app, they needed to keep some code private. How? “Choose tools that work with the grain of how you want to work, and work with the grain of your tools…”
npm, Inc.   Sponsor

.js is to join .exe, .bat, and other file types in being blocked for security reasons. You’ll need to archive or host them instead from February.
Google

Nearley is a fast and modern parser toolkit for JavaScript that uses the Earley algorithm and this is a handy walkthrough the ideas involved.
Gajus Kuizinas

What are ‘observables’? Why are they helpful for asynchronous communication? And how can you use them in an Angular 2 application with RxJS? Answers here.
Jen Looper

ES2017 is picking up some new concepts that will help with building higher-level concurrency abstractions. Here’s Axel with the deep dive.
Dr. Axel Rauschmayer

Regular expressions were added to JS 18 years ago and have only been slightly improved. Mathias looks at five new proposals under consideration to improve them further.
Mathias Bynens

Jobs Supported by Hired.com

  • Senior JavaScript Developer (Seattle)Love a challenge? Help us ship the fastest retail experience on the web. We value collaboration, passion for delivery, and work life balance.  Nordstrom
  • JavaScript Developer at X-Team (Remote)We're looking for a developer with an extensive knowledge of JavaScript and skills in different frameworks and libraries. We are 100% remote and we provide the funding needed to help you achieve your goals and grow. X-Team
  • Software Engineer, Web - Zürich, SwitzerlandCentralway is seeking an experienced programmer to come and join the team. The right candidate will be responsible for building and maintaining high performance web applications with cutting-edge technologies. Centralway Numbrs

Can't find the right job? Want companies to apply to you? Try Hired.com.

In Brief

Using Redux for Managing Immutable State tutorial
David Xu

JavaScript Array Methods: Mutating vs. Non-Mutating tutorial
Loren Stewart

Working with HTTP Streams with Cycle.js tutorial
Ivan Jovanovic

New Course: ⚡️ ES6 The Right Parts with Kyle Simpson course
Uncover new ES6 JS features like arrow functions, generators and computed properties with Kyle Simpson's book series: "You Don't Know JS".
Frontend Masters  Sponsor

A Look at TypeScript 2.1's Mapped Types tutorial
Create new types from existing ones by transforming properties.
Marius Schulz

How To Conditionally Load Modules with StealJS tutorial
Manuel Mujica

Source Maps with webpack in Chrome tutorial
Erik Aybar

JS Library Development in ES6 with Babel, Mocha and npm Scripts tutorial
Hugo Di Francesco

Getting Started with Vue.js 2.0 in 2017 for Beginners video
A tidy 20 minute intro and live coding demo.
Erik Hanchett

Code-Splitting Your Way to Better Performance with Webpack video
A quick 6 minute conversation.
Google Chrome Developers

ES6 and Beyond Workshop at PayPal (January 2017) video
Kent C Dodds

JSDOW: Chart Daily/Weekly Download Numbers for npm Packages tools
Who wants to add a virtual stock game to this? ;-)

Best in Class UI Components for Angular Development – Kendo UI for Angular code
100% jQuery-free Angular components built from ground-up which deliver the business app essential building blocks.
Progress  Sponsor

BuckleScript: A JavaScript Backend for the OCaml Compiler code
Bloomberg Finance

Tilt.js: Tiny Parallax Tilt Effect for jQuery code
Gijs Rogé

qart.js: Merges Pictures and QR Codes for Artistic QR Codes code
Clever, I’m surprised the output worked (I tested it!)
Lee Sun-Hyoup

Curated by Peter Cooper and published by Cooperpress.

Like this? You may also enjoy: FrontEnd Focus : Node Weekly : React Status

Stop getting JavaScript Weekly : Change email address : Read this issue on the Web

© Cooperpress Ltd. Office 30, Lincoln Way, Louth, LN11 0LS, UK


by via JavaScript Weekly