mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-11-22 12:32:09 +03:00
Compare commits
11 Commits
7d6958c290
...
c69f660734
Author | SHA1 | Date | |
---|---|---|---|
|
c69f660734 | ||
|
bc3598b1cd | ||
|
3c1b4e752d | ||
|
be43ada53d | ||
|
33e459864c | ||
|
54fbf0a948 | ||
|
fa95b37b6f | ||
|
572827b39f | ||
|
5d0fe40db8 | ||
|
234f4f52bb | ||
|
6c2f1c7132 |
@ -3,7 +3,7 @@ category: framework
|
||||
framework: AngularJS
|
||||
contributors:
|
||||
- ["Walter Cordero", "http://waltercordero.com"]
|
||||
filename: learnangular.html
|
||||
filename: learnangular.txt
|
||||
---
|
||||
|
||||
## AngularJS Tutorial.
|
||||
|
@ -1,7 +1,7 @@
|
||||
---
|
||||
language: HTML
|
||||
lang: ar-ar
|
||||
filename: learnhtml-tf.html
|
||||
filename: learnhtml-tf.txt
|
||||
contributors:
|
||||
- ["Christophe THOMAS", "https://github.com/WinChris"]
|
||||
translators:
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
language: HTML
|
||||
filename: html-ca.md
|
||||
filename: html-ca.txt
|
||||
contributors:
|
||||
- ["Christophe THOMAS", "https://github.com/WinChris"]
|
||||
translators:
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
language: HQ9+
|
||||
filename: hq9+-de.html
|
||||
filename: hq9+-de.txt
|
||||
contributors:
|
||||
- ["Alexey Nazaroff", "https://github.com/rogaven"]
|
||||
translators:
|
||||
|
@ -4,7 +4,7 @@ contributors:
|
||||
- ["Christophe THOMAS", "https://github.com/WinChris"]
|
||||
translators:
|
||||
- ["Dennis Keller", "https://github.com/denniskeller"]
|
||||
filename: learnhtml-de.html
|
||||
filename: learnhtml-de.txt
|
||||
lang: de-de
|
||||
---
|
||||
|
||||
|
@ -6,7 +6,6 @@ contributors:
|
||||
- ["Divay Prakash", "https://github.com/divayprakash"]
|
||||
translators:
|
||||
- ["caminsha", "https://github.com/caminsha"]
|
||||
filename: processing-de.md
|
||||
lang: de-de
|
||||
---
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
category: framework
|
||||
filename: learnshutit-de.html
|
||||
filename: learnshutit-de.py
|
||||
framework: ShutIt
|
||||
contributors:
|
||||
- ["Ian Miell", "http://ian.meirionconsulting.tk"]
|
||||
|
@ -2,7 +2,7 @@
|
||||
language: CSS
|
||||
contributors:
|
||||
- ["Kostas Bariotis", "http://kostasbariotis.com"]
|
||||
filename: css-gr.html.markdown
|
||||
filename: css-gr.css
|
||||
lang: el-gr
|
||||
---
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
language: HTML
|
||||
filename: learnhtml-gr.html
|
||||
filename: learnhtml-gr.txt
|
||||
contributors:
|
||||
- ["Dimitri Kokkonis", "https://github.com/kokkonisd"]
|
||||
lang: el-gr
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
language: JSON
|
||||
filename: json-gr.html.markdown
|
||||
filename: json-gr.json
|
||||
contributors:
|
||||
- ["Anna Harren", "https://github.com/iirelu"]
|
||||
- ["Marco Scannadinari", "https://github.com/marcoms"]
|
||||
|
@ -4,7 +4,7 @@ contributors:
|
||||
- ["Joao Pedrosa", "https://github.com/jpedrosa/"]
|
||||
translators:
|
||||
- ["Jorge Antonio Atempa", "http://www.twitter.com/atempa09"]
|
||||
filename: dart-es.md
|
||||
filename: dart-es.dart
|
||||
lang: es-es
|
||||
---
|
||||
|
||||
|
@ -5,7 +5,7 @@ contributors:
|
||||
translators:
|
||||
- ["Jhoon Saravia", "https://github.com/jhoon"]
|
||||
lang: es-es
|
||||
filename: groovy-es.html
|
||||
filename: groovy-es.groovy
|
||||
---
|
||||
|
||||
Groovy - Un lenguaje dinámico para la plataforma Java. [Leer más aquí](http://www.groovy-lang.org/).
|
||||
|
@ -4,7 +4,7 @@ contributors:
|
||||
- ["Adit Bhargava", "http://adit.io"]
|
||||
translators:
|
||||
- ["Jorge Antonio Atempa", "http://www.twitter.com/atempa09"]
|
||||
filename: haskell-es.md
|
||||
filename: haskell-es.hs
|
||||
lang: es-es
|
||||
---
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
language: HQ9+
|
||||
filename: hq9+-es.html
|
||||
filename: hq9+-es.txt
|
||||
contributors:
|
||||
- ["Alexey Nazaroff", "https://github.com/rogaven"]
|
||||
translators:
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
language: HTML
|
||||
filename: learnhtml-es.html
|
||||
filename: learnhtml-es.txt
|
||||
contributors:
|
||||
- ["Christophe THOMAS", "https://github.com/WinChris"]
|
||||
translators:
|
||||
|
@ -3,7 +3,7 @@ language: Wolfram
|
||||
lang: es-es
|
||||
contributors:
|
||||
- ["Daniel Caballero", "http://github.com/danielcaballero796/"]
|
||||
filename: learnwolfram-es.md
|
||||
filename: learnwolfram-es.nb
|
||||
---
|
||||
|
||||
Wolfram es un lenguaje subyacente originalmente utilizado en Mathematica, pero ahora esta disponible para su uso en múltiples contextos.
|
||||
|
@ -41,7 +41,7 @@ program example ! declare a program called example.
|
||||
real :: v, x ! WARNING: default initial values are compiler dependent!
|
||||
real :: a = 3, b = 2E12, c = 0.01
|
||||
integer :: i, j, k = 1, m
|
||||
real, parameter :: PI = 3.1415926535897931 ! declare a constant.
|
||||
real, parameter :: PI = 3.14159265 ! declare a constant.
|
||||
logical :: y = .TRUE., n = .FALSE. ! boolean type.
|
||||
complex :: w = (0, 1) ! sqrt(-1)
|
||||
character(len=3) :: month ! string of 3 characters.
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
language: HTML
|
||||
filename: learnhtml-fr.html
|
||||
filename: learnhtml-fr.txt
|
||||
contributors:
|
||||
- ["Christophe THOMAS", "https://github.com/WinChris"]
|
||||
lang: fr-fr
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
language: HTML
|
||||
filename: learnhtml-he.html
|
||||
filename: learnhtml-he.txt
|
||||
contributors:
|
||||
- ['Christophe THOMAS', 'https://github.com/WinChris']
|
||||
translators:
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
language: HQ9+
|
||||
filename: hq9+.html
|
||||
filename: hq9+.txt
|
||||
contributors:
|
||||
- ["Alexey Nazaroff", "https://github.com/rogaven"]
|
||||
---
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
language: HQ9+
|
||||
filename: hq9+-id.html
|
||||
filename: hq9+-id.txt
|
||||
contributors:
|
||||
- ["Alexey Nazaroff", "https://github.com/rogaven"]
|
||||
translators:
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
language: reStructuredText (RST)
|
||||
filename: rst-id.html
|
||||
filename: rst-id.rst
|
||||
contributors:
|
||||
- ["DamienVGN", "https://github.com/martin-damien"]
|
||||
- ["Andre Polykanine", "https://github.com/Oire"]
|
||||
|
@ -3,7 +3,7 @@ language: Rust
|
||||
contributors:
|
||||
- ["Carlo Milanesi", "http://github.com/carlomilanesi"]
|
||||
lang: it-it
|
||||
filename: rust-it.html.markdown
|
||||
filename: rust-it.rs
|
||||
---
|
||||
|
||||
Rust è un linguaggio di programmazione sviluppato da Mozilla Research.
|
||||
|
@ -225,7 +225,7 @@ public class LearnJava {
|
||||
String fooString = "My String Is Here!";
|
||||
|
||||
// Text blocks
|
||||
vat textBlock = """
|
||||
var textBlock = """
|
||||
This is a <Text Block> in Java
|
||||
""";
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
language: HTML
|
||||
filename: learnhtml-nl.html
|
||||
filename: learnhtml-nl.txt
|
||||
contributors:
|
||||
- ["Christophe THOMAS", "https://github.com/WinChris"]
|
||||
translators:
|
||||
|
@ -4,6 +4,7 @@ contributors:
|
||||
- ["Vincent van Wingerden", "https://github.com/vivanwin"]
|
||||
- ["Mariia Mykhailova", "https://github.com/tcNickolas"]
|
||||
- ["Andrew Ryan Davis", "https://github.com/AndrewDavis1191"]
|
||||
- ["Alex Hansen", "https://github.com/sezna"]
|
||||
filename: LearnQSharp.qs
|
||||
---
|
||||
|
||||
@ -16,35 +17,36 @@ Q# is a high-level domain-specific language which enables developers to write qu
|
||||
/////////////////////////////////////
|
||||
// 1. Quantum data types and operators
|
||||
|
||||
// The most important part of quantum programs is qubits.
|
||||
// The most important part of quantum programs is qubits.
|
||||
// In Q# type Qubit represents the qubits which can be used.
|
||||
// This will allocate an array of two new qubits as the variable qs.
|
||||
using (qs = Qubit[2]) {
|
||||
operation QuantumDataTypes() : Unit {
|
||||
use qs = Qubit[2];
|
||||
|
||||
// The qubits have internal state that you cannot access to read or modify directly.
|
||||
// You can inspect the current state of your quantum program
|
||||
// You can inspect the current state of your quantum program
|
||||
// if you're running it on a classical simulator.
|
||||
// Note that this will not work on actual quantum hardware!
|
||||
DumpMachine();
|
||||
Std.Diagnostics.DumpMachine();
|
||||
|
||||
// If you want to change the state of a qubit
|
||||
// you have to do this by applying quantum gates to the qubit.
|
||||
H(qs[0]); // This changes the state of the first qubit
|
||||
// from |0⟩ (the initial state of allocated qubits)
|
||||
// to (|0⟩ + |1⟩) / sqrt(2).
|
||||
H(qs[0]); // This changes the state of the first qubit
|
||||
// from |0⟩ (the initial state of allocated qubits)
|
||||
// to (|0⟩ + |1⟩) / sqrt(2).
|
||||
// qs[1] = |1⟩; - this does NOT work, you have to manipulate a qubit by using gates.
|
||||
|
||||
// You can apply multi-qubit gates to several qubits.
|
||||
CNOT(qs[0], qs[1]);
|
||||
|
||||
// You can also apply a controlled version of a gate:
|
||||
// You can also apply a controlled version of a gate:
|
||||
// a gate that is applied if all control qubits are in |1⟩ state.
|
||||
// The first argument is an array of control qubits,
|
||||
// The first argument is an array of control qubits,
|
||||
// the second argument is the target qubit.
|
||||
Controlled Y([qs[0]], qs[1]);
|
||||
Controlled Y([qs[0]], qs[1]);
|
||||
|
||||
// If you want to apply an anti-controlled gate
|
||||
// (a gate that is applied if all control qubits are in |0⟩ state),
|
||||
// If you want to apply an anti-controlled gate
|
||||
// (a gate that is applied if all control qubits are in |0⟩ state),
|
||||
// you can use a library function.
|
||||
ApplyControlledOnInt(0, X, [qs[0]], qs[1]);
|
||||
|
||||
@ -58,96 +60,101 @@ using (qs = Qubit[2]) {
|
||||
/////////////////////////////////////
|
||||
// 2. Classical data types and operators
|
||||
|
||||
// Numbers in Q# can be stored in Int, BigInt or Double.
|
||||
let i = 1; // This defines an Int variable i equal to 1
|
||||
let bi = 1L; // This defines a BigInt variable bi equal to 1
|
||||
let d = 1.0; // This defines a Double variable d equal to 1
|
||||
function ClassicalDataTypes() : Unit {
|
||||
// Numbers in Q# can be stored in Int, BigInt or Double.
|
||||
let i = 1; // This defines an Int variable i equal to 1
|
||||
let bi = 1L; // This defines a BigInt variable bi equal to 1
|
||||
let d = 1.0; // This defines a Double variable d equal to 1
|
||||
|
||||
// Arithmetic is done as expected, as long as the types are the same
|
||||
let n = 2 * 10; // = 20
|
||||
// Q# does not have implicit type cast,
|
||||
// so to perform arithmetic on values of different types,
|
||||
// you need to cast type explicitly
|
||||
let nd = IntAsDouble(2) * 1.0; // = 20.0
|
||||
// Arithmetic is done as expected, as long as the types are the same
|
||||
let n = 2 * 10; // = 20
|
||||
// Q# does not have implicit type cast,
|
||||
// so to perform arithmetic on values of different types,
|
||||
// you need to cast type explicitly
|
||||
let nd = Std.Convert.IntAsDouble(2) * 1.0; // = 20.0
|
||||
|
||||
// Boolean type is called Bool
|
||||
let trueBool = true;
|
||||
let falseBool = false;
|
||||
// Boolean type is called Bool
|
||||
let trueBool = true;
|
||||
let falseBool = false;
|
||||
|
||||
// Logic operators work as expected
|
||||
let andBool = true and false;
|
||||
let orBool = true or false;
|
||||
let notBool = not false;
|
||||
// Logic operators work as expected
|
||||
let andBool = true and false;
|
||||
let orBool = true or false;
|
||||
let notBool = not false;
|
||||
|
||||
// Strings
|
||||
let str = "Hello World!";
|
||||
// Strings
|
||||
let str = "Hello World!";
|
||||
|
||||
// Equality is ==
|
||||
let x = 10 == 15; // is false
|
||||
// Equality is ==
|
||||
let x = 10 == 15; // is false
|
||||
|
||||
// Range is a sequence of integers and can be defined like: start..step..stop
|
||||
let xi = 1..2..7; // Gives the sequence 1,3,5,7
|
||||
// Range is a sequence of integers and can be defined like: start..step..stop
|
||||
let xi = 1..2..7; // Gives the sequence 1,3,5,7
|
||||
|
||||
// Assigning new value to a variable:
|
||||
// by default all Q# variables are immutable;
|
||||
// if the variable was defined using let, you cannot reassign its value.
|
||||
// Assigning new value to a variable:
|
||||
// by default all Q# variables are immutable;
|
||||
// if the variable was defined using let, you cannot reassign its value.
|
||||
|
||||
// When you want to make a variable mutable, you have to declare it as such,
|
||||
// and use the set word to update value
|
||||
mutable xii = true;
|
||||
set xii = false;
|
||||
// When you want to make a variable mutable, you have to declare it as such,
|
||||
// and use the set word to update value
|
||||
mutable xii = true;
|
||||
set xii = false;
|
||||
|
||||
// You can create an array for any data type like this
|
||||
let xiii = new Double[10];
|
||||
// You can create an array for any data type like this
|
||||
let xiii = [0.0, size = 10];
|
||||
|
||||
// Getting an element from an array
|
||||
let xiv = xiii[8];
|
||||
// Getting an element from an array
|
||||
let xiv = xiii[8];
|
||||
|
||||
// Assigning a new value to an array element
|
||||
mutable xv = new Double[10];
|
||||
set xv w/= 5 <- 1;
|
||||
// Assigning a new value to an array element
|
||||
mutable xv = [0.0, size = 10];
|
||||
set xv w/= 5 <- 1.0;
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////
|
||||
// 3. Control flow
|
||||
|
||||
// If structures work a little different than most languages
|
||||
if (a == 1) {
|
||||
// ...
|
||||
} elif (a == 2) {
|
||||
// ...
|
||||
} else {
|
||||
// ...
|
||||
}
|
||||
operation ControlFlow() : Unit {
|
||||
let a = 1;
|
||||
// If expressions support a true branch, elif, and else.
|
||||
if (a == 1) {
|
||||
// ...
|
||||
} elif (a == 2) {
|
||||
// ...
|
||||
} else {
|
||||
// ...
|
||||
}
|
||||
use qubits = Qubit[2];
|
||||
|
||||
// Foreach loops can be used to iterate over an array
|
||||
for (qubit in qubits) {
|
||||
X(qubit);
|
||||
}
|
||||
// For loops can be used to iterate over an array
|
||||
for qubit in qubits {
|
||||
X(qubit);
|
||||
}
|
||||
|
||||
// Regular for loops can be used to iterate over a range of numbers
|
||||
for (index in 0 .. Length(qubits) - 1) {
|
||||
X(qubits[index]);
|
||||
}
|
||||
// Regular for loops can be used to iterate over a range of numbers
|
||||
for index in 0..Length(qubits) - 1 {
|
||||
X(qubits[index]);
|
||||
}
|
||||
|
||||
// While loops are restricted for use in classical context only
|
||||
mutable index = 0;
|
||||
while (index < 10) {
|
||||
set index += 1;
|
||||
}
|
||||
// While loops are restricted for use in classical context only
|
||||
mutable index = 0;
|
||||
while (index < 10) {
|
||||
set index += 1;
|
||||
}
|
||||
|
||||
// Quantum equivalent of a while loop is a repeat-until-success loop.
|
||||
// Because of the probabilistic nature of quantum computing sometimes
|
||||
// you want to repeat a certain sequence of operations
|
||||
// until a specific condition is achieved; you can use this loop to express this.
|
||||
repeat {
|
||||
// Your operation here
|
||||
let success_criteria = true;
|
||||
// Quantum equivalent of a while loop is a repeat-until-success loop.
|
||||
// Because of the probabilistic nature of quantum computing sometimes
|
||||
// you want to repeat a certain sequence of operations
|
||||
// until a specific condition is achieved; you can use this loop to express this.
|
||||
repeat {
|
||||
// Your operation here
|
||||
} until (success_criteria) // This could be a measurement to check if the state is reached
|
||||
fixup {
|
||||
// Resetting to the initial conditions, if required
|
||||
}
|
||||
}
|
||||
until (success criteria) // This could be a measurement to check if the state is reached
|
||||
fixup {
|
||||
// Resetting to the initial conditions, if required
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////
|
||||
// 4. Putting it all together
|
||||
@ -157,11 +164,11 @@ operation ApplyXGate(source : Qubit) : Unit {
|
||||
X(source);
|
||||
}
|
||||
|
||||
// If the operation implements a unitary transformation, you can define
|
||||
// adjoint and controlled variants of it.
|
||||
// The easiest way to do that is to add "is Adj + Ctl" after Unit.
|
||||
// If the operation implements a unitary transformation, you can define
|
||||
// adjoint and controlled variants of it.
|
||||
// The easiest way to do that is to add "is Adj + Ctl" after Unit.
|
||||
// This will tell the compiler to generate the variants automatically.
|
||||
operation ApplyXGateCA (source : Qubit) : Unit is Adj + Ctl {
|
||||
operation ApplyXGateCA(source : Qubit) : Unit is Adj + Ctl {
|
||||
X(source);
|
||||
}
|
||||
|
||||
@ -169,20 +176,21 @@ operation ApplyXGateCA (source : Qubit) : Unit is Adj + Ctl {
|
||||
|
||||
|
||||
// To run Q# code, you can put @EntryPoint() before the operation you want to run first
|
||||
@EntryPoint()
|
||||
operation XGateDemo() : Unit {
|
||||
using (q = Qubit()) {
|
||||
ApplyXGate(q);
|
||||
}
|
||||
use q = Qubit();
|
||||
ApplyXGate(q);
|
||||
}
|
||||
|
||||
// Here is a simple example: a quantum random number generator.
|
||||
// Here is a simple example: a quantum random number generator.
|
||||
// We will generate a classical array of random bits using quantum code.
|
||||
@EntryPoint()
|
||||
operation QRNGDemo() : Unit {
|
||||
mutable bits = new Int[5]; // Array we'll use to store bits
|
||||
using (q = Qubit()) { // Allocate a qubit
|
||||
for (i in 0 .. 4) { // Generate each bit independently
|
||||
// Callables (functions or operations) named `Main` are used as entry points.
|
||||
operation Main() : Unit {
|
||||
mutable bits = [0, size = 5]; // Array we'll use to store bits
|
||||
use q = Qubit();
|
||||
{
|
||||
// Allocate a qubit
|
||||
for i in 0..4 {
|
||||
// Generate each bit independently
|
||||
H(q); // Hadamard gate sets equal superposition
|
||||
let result = M(q); // Measure qubit gets 0|1 with 50/50 prob
|
||||
let bit = result == Zero ? 0 | 1; // Convert measurement result to integer
|
||||
@ -196,9 +204,6 @@ operation QRNGDemo() : Unit {
|
||||
|
||||
## Further Reading
|
||||
|
||||
The [Quantum Katas][1] offer great self-paced tutorials and programming exercises to learn quantum computing and Q#.
|
||||
The Quantum Katas ([repo](https://github.com/microsoft/qsharp/tree/main/katas) [hosted tutorials](https://quantum.microsoft.com/en-us/tools/quantum-katas) offer great self-paced tutorials and programming exercises to learn quantum computing and Q#.
|
||||
|
||||
[Q# Documentation][2] is official Q# documentation, including language reference and user guides.
|
||||
|
||||
[1]: https://github.com/microsoft/QuantumKatas
|
||||
[2]: https://docs.microsoft.com/quantum/
|
||||
[Q# Documentation](https://docs.microsoft.com/quantum/) is official Q# documentation, including language reference and user guides.
|
||||
|
@ -5,7 +5,7 @@ contributors:
|
||||
- ["Mathias Bynens", "http://mathiasbynens.be/"]
|
||||
translators:
|
||||
- ["Petru Dimitriu", "http://petru-dimitriu.github.io"]
|
||||
filename: bf-ro.html
|
||||
filename: bf-ro.bf
|
||||
lang: ro-ro
|
||||
---
|
||||
|
||||
|
@ -5,7 +5,7 @@ contributors:
|
||||
translators:
|
||||
- ["Petru Dimitriu", "http://petru-dimitriu.github.io"]
|
||||
lang: ro-ro
|
||||
filename: haskell-ro.html
|
||||
filename: haskell-ro.hs
|
||||
---
|
||||
|
||||
Haskell este un limbaj de programare practic, pur funcțional.
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
language: HTML
|
||||
filename: learnhtml-ru.html
|
||||
filename: learnhtml-ru.txt
|
||||
contributors:
|
||||
- ["Christophe THOMAS", "https://github.com/WinChris"]
|
||||
translators:
|
||||
|
@ -91,7 +91,7 @@ false.class #=> FalseClass
|
||||
2 <= 2 #=> true
|
||||
2 >= 2 #=> true
|
||||
|
||||
# Combined comparison operator (returns `1` when the first argument is greater,
|
||||
# Combined comparison operator (returns `1` when the first argument is greater,
|
||||
# `-1` when the second argument is greater, and `0` otherwise)
|
||||
1 <=> 10 #=> -1 (1 < 10)
|
||||
10 <=> 1 #=> 1 (10 > 1)
|
||||
@ -259,8 +259,8 @@ else
|
||||
'else, also optional'
|
||||
end
|
||||
|
||||
# If a condition controls invocation of a single statement rather than a block of code
|
||||
# you can use postfix-if notation
|
||||
# If a condition controls invocation of a single statement rather than a block
|
||||
# of code you can use postfix-if notation
|
||||
warnings = ['Patronimic is missing', 'Address too short']
|
||||
puts("Some warnings occurred:\n" + warnings.join("\n")) if !warnings.empty?
|
||||
|
||||
@ -268,7 +268,7 @@ puts("Some warnings occurred:\n" + warnings.join("\n")) if !warnings.empty?
|
||||
puts("Some warnings occurred:\n" + warnings.join("\n")) unless warnings.empty?
|
||||
|
||||
# Loops
|
||||
# In Ruby, traditional `for` loops aren't very common. Instead, these
|
||||
# In Ruby, traditional `for` loops aren't very common. Instead, these
|
||||
# basic loops are implemented using enumerable, which hinges on `each`.
|
||||
(1..5).each do |counter|
|
||||
puts "iteration #{counter}"
|
||||
@ -279,9 +279,10 @@ for counter in 1..5
|
||||
puts "iteration #{counter}"
|
||||
end
|
||||
|
||||
# The `do |variable| ... end` construct above is called a 'block'. Blocks are similar
|
||||
# to lambdas, anonymous functions or closures in other programming languages. They can
|
||||
# be passed around as objects, called, or attached as methods.
|
||||
# The `do |variable| ... end` construct above is called a 'block'. Blocks are
|
||||
# similar to lambdas, anonymous functions or closures in other programming
|
||||
# languages. They can be passed around as objects, called, or attached as
|
||||
# methods.
|
||||
#
|
||||
# The 'each' method of a range runs the block once for each element of the range.
|
||||
# The block is passed a counter as a parameter.
|
||||
@ -415,19 +416,20 @@ surround { puts 'hello world' }
|
||||
#=> hello world
|
||||
#=> }
|
||||
|
||||
# Blocks can be converted into a 'proc' object, which wraps the block
|
||||
# and allows it to be passed to another method, bound to a different scope,
|
||||
# or manipulated otherwise. This is most common in method parameter lists,
|
||||
# where you frequently see a trailing '&block' parameter that will accept
|
||||
# the block, if one is given, and convert it to a 'Proc'. The naming here is
|
||||
# convention; it would work just as well with '&pineapple'.
|
||||
# Blocks can be converted into a 'proc' object, which wraps the block and allows
|
||||
# it to be passed to another method, bound to a different scope, or manipulated
|
||||
# otherwise. This is most common in method parameter lists, where you frequently
|
||||
# see a trailing '&block' parameter that will accept the block, if one is given,
|
||||
# and convert it to a 'Proc'. The naming here is convention; it would work just
|
||||
# as well with '&pineapple'.
|
||||
def guests(&block)
|
||||
block.class #=> Proc
|
||||
block.call(4)
|
||||
end
|
||||
|
||||
# The 'call' method on the Proc is similar to calling 'yield' when a block is
|
||||
# present. The arguments passed to 'call' will be forwarded to the block as arguments.
|
||||
# The 'call' method on the Proc is similar to calling 'yield' when a block is
|
||||
# present. The arguments passed to 'call' will be forwarded to the block as
|
||||
# arguments.
|
||||
|
||||
guests { |n| "You have #{n} guests." }
|
||||
# => "You have 4 guests."
|
||||
@ -443,7 +445,7 @@ end
|
||||
upcased = ['Watch', 'these', 'words', 'get', 'upcased'].map(&:upcase)
|
||||
puts upcased
|
||||
#=> ["WATCH", "THESE", "WORDS", "GET", "UPCASED"]
|
||||
|
||||
|
||||
sum = [1, 2, 3, 4, 5].reduce(&:+)
|
||||
puts sum
|
||||
#=> 15
|
||||
@ -472,7 +474,7 @@ def best(first, second, third, *others)
|
||||
puts "There were #{others.count} other participants."
|
||||
end
|
||||
|
||||
best *ranked_competitors
|
||||
best *ranked_competitors
|
||||
#=> Winners are John, Sally, and Dingus.
|
||||
#=> There were 2 other participants.
|
||||
|
||||
@ -480,9 +482,9 @@ best *ranked_competitors
|
||||
5.even? #=> false
|
||||
5.odd? #=> true
|
||||
|
||||
# By convention, if a method name ends with an exclamation mark, it does something destructive
|
||||
# like mutate the receiver. Many methods have a ! version to make a change, and
|
||||
# a non-! version to just return a new changed version.
|
||||
# By convention, if a method name ends with an exclamation mark, it does
|
||||
# something destructive like mutate the receiver. Many methods have a ! version
|
||||
# to make a change, and a non-! version to just return a new changed version.
|
||||
company_name = "Dunder Mifflin"
|
||||
company_name.upcase #=> "DUNDER MIFFLIN"
|
||||
company_name #=> "Dunder Mifflin"
|
||||
@ -516,7 +518,8 @@ class Human
|
||||
@name
|
||||
end
|
||||
|
||||
# The above functionality can be encapsulated using the attr_accessor method as follows.
|
||||
# The above functionality can be encapsulated using the attr_accessor method
|
||||
# as follows.
|
||||
attr_accessor :name
|
||||
|
||||
# Getter/setter methods can also be created individually like this.
|
||||
|
@ -3,7 +3,7 @@ category: framework
|
||||
framework: ShutIt
|
||||
contributors:
|
||||
- ["Ian Miell", "http://ian.meirionconsulting.tk"]
|
||||
filename: learnshutit.html
|
||||
filename: learnshutit.py
|
||||
---
|
||||
|
||||
## ShutIt
|
||||
|
@ -37,6 +37,11 @@ let notSure: any = 4;
|
||||
notSure = "maybe a string instead";
|
||||
notSure = false; // okay, definitely a boolean
|
||||
|
||||
// When we are not sure about the type, and don't want to use "any", we can use "unknown" type
|
||||
let unknownVariable: unknown = 'Some string'; // we don't fully know yet the type
|
||||
unknownVariable = 2; // possible
|
||||
const someVariable: string = unknownVariable; // Invalid; we can't assign unknownVariable to any other type (without an explicit assertion), unlike any
|
||||
|
||||
// Use const keyword for constants
|
||||
const numLivesForCat = 9;
|
||||
numLivesForCat = 1; // Error
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
language: HTML
|
||||
filename: learnhtml-vi.html
|
||||
filename: learnhtml-vi.txt
|
||||
contributors:
|
||||
- ["Christophe THOMAS", "https://github.com/WinChris"]
|
||||
translators:
|
||||
|
@ -58,6 +58,7 @@ The syntax for tables is [very complicated](https://en.wikipedia.org/wiki/Help:T
|
||||
```
|
||||
|
||||
which renders to
|
||||
|
||||
| **column title A** | **column title B** |
|
||||
|---|---|
|
||||
| cell A1 | cell B1 |
|
||||
@ -120,6 +121,7 @@ If a word is interrupted by a link, it is "blended" into the link. For example,
|
||||
To suppress this behavior, use `<nowiki>`. For example, `[[micro-]]<nowiki />second` renders to [micro-](https://en.wikipedia.org/wiki/micro-)second.
|
||||
|
||||
There are three kinds of external linking. The third kind is preferred:
|
||||
|
||||
| wikitext | renders to |
|
||||
|----|----|
|
||||
| `https://www.wikipedia.org` | [https://www.wikipedia.org](https://www.wikipedia.org) |
|
||||
|
@ -3,7 +3,7 @@ category: framework
|
||||
framework: AngularJS
|
||||
contributors:
|
||||
- ["Walter Cordero", "http://waltercordero.com"]
|
||||
filename: learnangular-cn.html
|
||||
filename: learnangular-cn.txt
|
||||
translators:
|
||||
- ["Jiang Haiyun", "http://www.atjiang.com"]
|
||||
lang: zh-cn
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
language: HTML
|
||||
filename: learnhtml-cn.html
|
||||
filename: learnhtml-cn.txt
|
||||
contributors:
|
||||
- ["Christophe THOMAS", "https://github.com/WinChris"]
|
||||
translators:
|
||||
|
@ -5,7 +5,7 @@ contributors:
|
||||
- ["David Pedersen", "http://lonelyproton.com/"]
|
||||
- ["James Baker", "http://www.jbaker.io/"]
|
||||
- ["Leo Zovic", "http://langnostic.inaimathi.ca/"]
|
||||
filename: standard-ml-cn.html
|
||||
filename: standard-ml-cn.sml
|
||||
translators:
|
||||
- ["Buqian Zheng", "https://github.com/zhengbuqian"]
|
||||
lang: zh-cn
|
||||
|
Loading…
Reference in New Issue
Block a user