mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-06 14:56:54 +02:00
Added two minor changes in integer division to make it clear
This commit is contained in:
@@ -7,6 +7,7 @@ contributors:
|
|||||||
- ["Alexandre Medeiros", "http://alemedeiros.sdf.org"]
|
- ["Alexandre Medeiros", "http://alemedeiros.sdf.org"]
|
||||||
- ["Denis Arh", "https://github.com/darh"]
|
- ["Denis Arh", "https://github.com/darh"]
|
||||||
- ["akirahirose", "https://twitter.com/akirahirose"]
|
- ["akirahirose", "https://twitter.com/akirahirose"]
|
||||||
|
- ["Anton Strömkvist", "http://lutic.org/"]
|
||||||
filename: LearnBash.sh
|
filename: LearnBash.sh
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -81,6 +82,17 @@ fi
|
|||||||
echo "Always executed" || echo "Only executed if first command fails"
|
echo "Always executed" || echo "Only executed if first command fails"
|
||||||
echo "Always executed" && echo "Only executed if first command does NOT fail"
|
echo "Always executed" && echo "Only executed if first command does NOT fail"
|
||||||
|
|
||||||
|
# To use && and || with if statements, you need multiple pairs of square brackets:
|
||||||
|
if [ $NAME == "Steve" ] && [ $AGE -eq 15 ]
|
||||||
|
then
|
||||||
|
echo "This will run if $NAME is Steve AND $AGE is 15."
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ $NAME == "Daniya" ] || [ $NAME == "Zach" ]
|
||||||
|
then
|
||||||
|
echo "This will run if $NAME is Daniya OR Zach."
|
||||||
|
fi
|
||||||
|
|
||||||
# Expressions are denoted with the following format:
|
# Expressions are denoted with the following format:
|
||||||
echo $(( 10 + 5 ))
|
echo $(( 10 + 5 ))
|
||||||
|
|
||||||
|
@@ -92,7 +92,7 @@ sub as-many($head, *@rest) { # the `*@` slurpy will basically "take everything e
|
|||||||
# but not *after*.
|
# but not *after*.
|
||||||
say @rest.join(' / ') ~ " !";
|
say @rest.join(' / ') ~ " !";
|
||||||
}
|
}
|
||||||
say as-many('Happy', 'Happy', 'Birthday'); #=> Happy Birthday !
|
say as-many('Happy', 'Happy', 'Birthday'); #=> Happy / Birthday !
|
||||||
# Note that the splat did not consume the parameter before.
|
# Note that the splat did not consume the parameter before.
|
||||||
|
|
||||||
## You can call a function with an array using the "argument list flattening" operator `|`
|
## You can call a function with an array using the "argument list flattening" operator `|`
|
||||||
@@ -127,7 +127,7 @@ sub with-named($normal-arg, :$named) {
|
|||||||
}
|
}
|
||||||
with-named(1, named => 6); #=> 7
|
with-named(1, named => 6); #=> 7
|
||||||
# There's one gotcha to be aware of, here:
|
# There's one gotcha to be aware of, here:
|
||||||
# If you quote your key, Perl 6 won't be able to see it as compile time,
|
# If you quote your key, Perl 6 won't be able to see it at compile time,
|
||||||
# and you'll have a single Pair object as a positional paramater.
|
# and you'll have a single Pair object as a positional paramater.
|
||||||
|
|
||||||
with-named(2, :named(5)); #=> 7
|
with-named(2, :named(5)); #=> 7
|
||||||
@@ -171,9 +171,9 @@ named-def(def => 15); #=> 15
|
|||||||
|
|
||||||
### Containers
|
### Containers
|
||||||
# In Perl 6, values are actually stored in "containers".
|
# In Perl 6, values are actually stored in "containers".
|
||||||
# the assignment operator asks the container on the left to store the value on its right
|
# The assignment operator asks the container on the left to store the value on its right.
|
||||||
# When passed around, containers are marked as immutable. Which means that, in a function,
|
# When passed around, containers are marked as immutable. Which means that, in a function,
|
||||||
# you'll get an error if you try to mutate one of your argument.
|
# you'll get an error if you try to mutate one of your arguments.
|
||||||
# If you really need to, you can ask for a mutable container using `is rw` :
|
# If you really need to, you can ask for a mutable container using `is rw` :
|
||||||
sub mutate($n is rw) {
|
sub mutate($n is rw) {
|
||||||
$n++;
|
$n++;
|
||||||
@@ -374,7 +374,7 @@ sub foo(@array [$fst, $snd]) {
|
|||||||
say "My first is $fst, my second is $snd ! All in all, I'm @array[].";
|
say "My first is $fst, my second is $snd ! All in all, I'm @array[].";
|
||||||
# (^ remember the `[]` to interpolate the array)
|
# (^ remember the `[]` to interpolate the array)
|
||||||
}
|
}
|
||||||
foo(@tail); #=> My first is 2, my second is 3 ! All in all, I'm 1 2
|
foo(@tail); #=> My first is 2, my second is 3 ! All in all, I'm 2 3
|
||||||
|
|
||||||
|
|
||||||
# If you're not using the array itself, you can also keep it anonymous, much like a scalar:
|
# If you're not using the array itself, you can also keep it anonymous, much like a scalar:
|
||||||
|
@@ -441,7 +441,10 @@ class Human(object):
|
|||||||
# A class attribute. It is shared by all instances of this class
|
# A class attribute. It is shared by all instances of this class
|
||||||
species = "H. sapiens"
|
species = "H. sapiens"
|
||||||
|
|
||||||
# Basic initializer
|
# Basic initializer, this is called when this class is instantiated.
|
||||||
|
# Note that the double leading and trailing underscores denote objects
|
||||||
|
# or attributes that are used by python but that live in user-controlled
|
||||||
|
# namespaces. You should not invent such names on your own.
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
# Assign the argument to the instance's name attribute
|
# Assign the argument to the instance's name attribute
|
||||||
self.name = name
|
self.name = name
|
||||||
@@ -528,10 +531,12 @@ def double_numbers(iterable):
|
|||||||
# Note xrange is a generator that does the same thing range does.
|
# Note xrange is a generator that does the same thing range does.
|
||||||
# Creating a list 1-900000000 would take lot of time and space to be made.
|
# Creating a list 1-900000000 would take lot of time and space to be made.
|
||||||
# xrange creates an xrange generator object instead of creating the entire list like range does.
|
# xrange creates an xrange generator object instead of creating the entire list like range does.
|
||||||
_xrange = xrange(1, 900000000)
|
# We use a trailing underscore in variable names when we want to use a name that
|
||||||
|
# would normally collide with a python keyword
|
||||||
|
xrange_ = xrange(1, 900000000)
|
||||||
|
|
||||||
# will double all numbers until a result >=30 found
|
# will double all numbers until a result >=30 found
|
||||||
for i in double_numbers(_xrange):
|
for i in double_numbers(xrange_):
|
||||||
print(i)
|
print(i)
|
||||||
if i >= 30:
|
if i >= 30:
|
||||||
break
|
break
|
||||||
@@ -544,10 +549,10 @@ for i in double_numbers(_xrange):
|
|||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
||||||
|
|
||||||
def beg(_say):
|
def beg(target_function):
|
||||||
@wraps(_say)
|
@wraps(target_function)
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
msg, say_please = _say(*args, **kwargs)
|
msg, say_please = target_function(*args, **kwargs)
|
||||||
if say_please:
|
if say_please:
|
||||||
return "{} {}".format(msg, "Please! I am poor :(")
|
return "{} {}".format(msg, "Please! I am poor :(")
|
||||||
return msg
|
return msg
|
||||||
|
@@ -470,7 +470,10 @@ class Human(object):
|
|||||||
# A class attribute. It is shared by all instances of this class
|
# A class attribute. It is shared by all instances of this class
|
||||||
species = "H. sapiens"
|
species = "H. sapiens"
|
||||||
|
|
||||||
# Basic initializer
|
# Basic initializer, this is called when this class is instantiated.
|
||||||
|
# Note that the double leading and trailing underscores denote objects
|
||||||
|
# or attributes that are used by python but that live in user-controlled
|
||||||
|
# namespaces. You should not invent such names on your own.
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
# Assign the argument to the instance's name attribute
|
# Assign the argument to the instance's name attribute
|
||||||
self.name = name
|
self.name = name
|
||||||
@@ -556,9 +559,11 @@ def double_numbers(iterable):
|
|||||||
# double_numbers.
|
# double_numbers.
|
||||||
# Note range is a generator too. Creating a list 1-900000000 would take lot of
|
# Note range is a generator too. Creating a list 1-900000000 would take lot of
|
||||||
# time to be made
|
# time to be made
|
||||||
_range = range(1, 900000000)
|
# We use a trailing underscore in variable names when we want to use a name that
|
||||||
|
# would normally collide with a python keyword
|
||||||
|
range_ = range(1, 900000000)
|
||||||
# will double all numbers until a result >=30 found
|
# will double all numbers until a result >=30 found
|
||||||
for i in double_numbers(_range):
|
for i in double_numbers(range_):
|
||||||
print(i)
|
print(i)
|
||||||
if i >= 30:
|
if i >= 30:
|
||||||
break
|
break
|
||||||
@@ -571,10 +576,10 @@ for i in double_numbers(_range):
|
|||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
||||||
|
|
||||||
def beg(_say):
|
def beg(target_function):
|
||||||
@wraps(_say)
|
@wraps(target_function)
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
msg, say_please = _say(*args, **kwargs)
|
msg, say_please = target_function(*args, **kwargs)
|
||||||
if say_please:
|
if say_please:
|
||||||
return "{} {}".format(msg, "Please! I am poor :(")
|
return "{} {}".format(msg, "Please! I am poor :(")
|
||||||
return msg
|
return msg
|
||||||
|
Reference in New Issue
Block a user