﻿ Are There Any Additional Inject Shorthand - ITCodar

# Are There Any Additional Inject Shorthand

## are there any additional inject shorthand

From the doc on `Enumerable#inject`:

... If you specify a symbol instead, then each element in the collection will be passed to the named method of memo. In either case, the result becomes the new value for memo. At the end of the iteration, the final value of memo is the return value for the method.

If you do not explicitly specify an initial value for memo, then uses the first element of collection is used as the initial value of memo.

So, if you specify a symbol, it treats it as a method name and invokes this method on every element of the enumerable, replacing memo as stated above. Now, the math operators (+-*/) are just methods, nothing else. These lines produce identical result:

``13 + 23 # => 3613.+(23) # => 3613.send(:+, 23) # => 36``

When you pass a symbol to `inject` or `reduce` it uses the third form to dynamically apply that operator to elements:

``[1,2,3].inject(:+) # => 6``

This shorthand can be used with methods other than operators as well:

``[{"a"=>1}, {"b"=>2}].inject(:merge) # => {"a"=>1, "b"=>2} ``

## Is there a shorthand for addition assignment operator for nullables that sets the value if null?

You could put ternary operator:

``double? sum = null;sum = sum == null ? someTempValue : sum + someTempValue;``

Or

``sum = someTempValue + (sum == null ? 0 : sum);``

Or

``sum = someTempValue + (sum ?? 0);``

Credit to: Dmitry Bychenko for the last option

## How do I add 2 elements for each iteration in a python shorthand list?

Updated:

``[ i*k for i in range(10) for k in (2, 3) ]``

Original:

Just keeping it simple with list comprehension:

``[ i*k for i in range(10) for k in range(2,4) ]``

## Advice needed on shorthand if else shorthand

``day += -7 if day == 7 else 1``

The way you read this is "Add negative 7 to `day` if `day == 7`, otherwise, add 1 to `day`"

Dagorodir's original answer does not work, because it will subtract (`day` + 1) from the current value of day if `day != 7`. So using your example with the the starting value of 5 for `day`, the result of running the code from the other answer is `-1`.

## Is it possible to add data to option using shorthand?

Three ways to do this:

``\$(el).append(\$('<option>', {    value: index,    text: column_persistence[index],    "data-alt": check_box_id}));``

Or:

``\$(el).append(\$('<option>', {    value: index,    text: column_persistence[index]}).data("alt", check_box_id));``

Or:

``\$(el).append(\$('<option>', {    value: index,    text: column_persistence[index]}).attr("data-alt", check_box_id));``

These should result in the HTML Element:

``<option value="1" data-alt="chbk-1">Column Name</option>``

## Multi-Insert Shorthand with Primary Key & Identity

You can insert multiple rows like this:

``INSERT INTO `People` (`name`,`email`) VALUES ('George', 'George@test.com'),('Mary', 'LittleLamb@test.com');``

EDIT:

``mysql> create table `test`(    -> `id` int(10) unsigned not null AUTO_INCREMENT,    -> `name` varchar(255) not null default 'N/A/',    -> `email` varchar(255) not null default 'N/A/',    -> PRIMARY KEY(`id`)     -> )ENGINE=MyISAM DEFAULT CHARSET=latin1;Query OK, 0 rows affected (0.02 sec)mysql> INSERT INTO `test` VALUES (null, 'Name 1', 'Email 1'),(null, 'Name 2','Email 2');Query OK, 2 rows affected (0.00 sec)Records: 2  Duplicates: 0  Warnings: 0mysql> select * from `test`;+----+--------+---------+| id | name   | email   |+----+--------+---------+|  1 | Name 1 | Email 1 ||  2 | Name 2 | Email 2 |+----+--------+---------+2 rows in set (0.00 sec)``

As long as your primary key is set to auto increment, you can nullify the field and it will auto set the value to the auto increment value.

## Shorthand syntax to push or create item to array, at certain key of object

You can do something similar to the other answer, using an empty array as a default and `concat`:

``const obj = {}id = '4'obj[id] = (obj[id] || []).concat(['test'])console.log(obj)obj[id] = (obj[id] || []).concat(['test2'])console.log(obj)``

## Can't use shorthand arrow function

You don't need to specify `return` if you're using the shorthand arrow syntax, as it will implicitly return the result of the expression on the right hand side.

``gulp.task('devInject', ['styles', 'devScripts'], () => inject());``

Of course, this means if you ever want to extend the arrow function to have multiple expressions, you'll have to add the `{` and `}` back in and use an explicit return statement.