Skip to content

Reorder tour; a script for reordering #307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ pygments: true
permalink: /:categories/:title.html
baseurl:

# markdown: rdiscount
exclude:
- renumber.rb

# markdown: rdiscount
107 changes: 107 additions & 0 deletions renumber.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/usr/bin/env ruby

# The workflow is pretty straightforward for using this script to
# reorganize a multi-file document.
#
# 1. Have a clean tree.
# 2. Use -x *.md to print an index; write that to a file.
# 3. Reorder the lines in the file to match your desired order.
# 4. Feed the index back in with -i.

require 'optparse'

def opts(args)
options = {:encoding => 'UTF-8'}
OptionParser.new do |opts|
opts.banner = "Usage: ./renumber.rb [OPTIONS] [-i INDEX | FILES]"

opts.on("-i", "--index FILE", "Use newline-separated list of files") do |i|
options[:index] = i
end
opts.on("-e", "--encoding ENC", "Set character encoding") do |e|
options[:encoding] = e
end
opts.on("-x", "--print-index", "Show index, don't change any files") do |x|
options[:print_index] = true
end
opts.on("-s", "--start-from NUM", Integer, "Use fresh numbers instead of reorganizing the existing numbers") do |s|
options[:start_from] = s
end
end.parse!(args)
options
end

# `f`'s number and the num index location from its lines, or nil if
# none found.
def filenum(lines)
marker = /^---\s*/
s = lines.index {|l| marker =~ l}
if s
e = lines.drop(s+1).index {|l| marker =~ l}
if e
nl = lines[s+1, e].map {|l| /^num: (\d+)\s*/.match(l)}
nli = nl.index {|l| l}
if nli
[nl[nli][1].to_i, s + 1 + nli]
end
end
end
end

def setnum(n, nli, lines, fname)
File.open(fname, "w") do |f|
lines[0, nli].each {|l| f.write(l)}
f.write("num: #{n}\n")
lines[nli+1..-1].each {|l| f.write(l)}
end
end

# List of (filename, number, number line index, lines) for each of
# files, or nil for entries with no number.
def filenums(files)
files.map do |fname|
lines = File.open(fname) {|f| f.readlines}
fn = filenum(lines)
if fn
[fname] + fn + [lines]
else
warn("#{fname} has no num: index entry; skipping")
end
end
end

# Given filenums result and an optional number from which to create
# start new indices, write new indices.
def rewrite_indices(ns, start_from)
nums = if start_from
start_from..(start_from + ns.size - 1)
else
ns.map{|e| e[1]}.sort
end
nums.zip(ns) do |e|
nn, oe = e
fn, n, nli, lines = oe
setnum(nn, nli, lines, fn)
end
end

def print_index(ns)
ns.sort{|x,y| x[1] <=> y[1]}.each{|n| puts(n[0])}
end

def main(args)
o = opts(args)
fileset = if o[:index]
File.open(o[:index]) {|f| f.readlines}.map{|fn| fn.chomp}
else
args
end
ns = filenums(fileset).select{|e| e}
if o[:print_index]
print_index(ns)
else
rewrite_indices(ns, o[:start_from])
end
end

main(ARGV)
2 changes: 1 addition & 1 deletion tutorials/tour/abstract-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Abstract Types
disqus: true

tutorial: scala-tour
num: 2
num: 21
outof: 35
languages: [es]
---
Expand Down
2 changes: 1 addition & 1 deletion tutorials/tour/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Annotations
disqus: true

tutorial: scala-tour
num: 3
num: 30
---

Annotations associate meta-information with definitions.
Expand Down
2 changes: 1 addition & 1 deletion tutorials/tour/anonymous-function-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Anonymous Function Syntax
disqus: true

tutorial: scala-tour
num: 14
num: 6
---

Scala provides a relatively lightweight syntax for defining anonymous functions. The following expression creates a successor function for integers:
Expand Down
2 changes: 1 addition & 1 deletion tutorials/tour/automatic-closures.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Automatic Type-Dependent Closure Construction
disqus: true

tutorial: scala-tour
num: 16
num: 29
---

Scala allows parameterless function names as parameters of methods. When such a method is called, the actual parameters for parameterless function names are not evaluated and a nullary function is passed instead which encapsulates the computation of the corresponding parameter (so-called *call-by-name* evalutation).
Expand Down
2 changes: 1 addition & 1 deletion tutorials/tour/case-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Case Classes
disqus: true

tutorial: scala-tour
num: 5
num: 10
---

Scala supports the notion of _case classes_. Case classes are regular classes which export their constructor parameters and which provide a recursive decomposition mechanism via [pattern matching](pattern-matching.html).
Expand Down
2 changes: 1 addition & 1 deletion tutorials/tour/classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Classes
disqus: true

tutorial: scala-tour
num: 4
num: 3
---

Classes in Scala are static templates that can be instantiated into many objects at runtime.
Expand Down
2 changes: 1 addition & 1 deletion tutorials/tour/compound-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Compound Types
disqus: true

tutorial: scala-tour
num: 6
num: 22
---

Sometimes it is necessary to express that the type of an object is a subtype of several other types. In Scala this can be expressed with the help of *compound types*, which are intersections of object types.
Expand Down
2 changes: 1 addition & 1 deletion tutorials/tour/currying.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Currying
disqus: true

tutorial: scala-tour
num: 15
num: 9
---

Methods may define multiple parameter lists. When a method is called with a fewer number of parameter lists, then this will yield a function taking the missing parameter lists as its arguments.
Expand Down
2 changes: 1 addition & 1 deletion tutorials/tour/default-parameter-values.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Default Parameter values
disqus: true

tutorial: scala-tour
num: 34
num: 31
---

Scala provides the ability to give parameters default values that can be used to allow a caller to omit those parameters.
Expand Down
2 changes: 1 addition & 1 deletion tutorials/tour/explicitly-typed-self-references.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Explicitly Typed Self References
disqus: true

tutorial: scala-tour
num: 27
num: 23
---

When developing extensible software it is sometimes handy to declare the type of the value `this` explicitly. To motivate this, we will derive a small extensible representation of a graph data structure in Scala.
Expand Down
2 changes: 1 addition & 1 deletion tutorials/tour/extractor-objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Extractor Objects
disqus: true

tutorial: scala-tour
num: 8
num: 14
---

In Scala, patterns can be defined independently of case classes. To this end, a method named unapply is defined to yield a so-called extractor. For instance, the following code defines an extractor object Twice.
Expand Down
2 changes: 1 addition & 1 deletion tutorials/tour/generic-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Generic Classes
disqus: true

tutorial: scala-tour
num: 9
num: 16
---

Like in Java 5 (aka. [JDK 1.5](http://java.sun.com/j2se/1.5/)), Scala has built-in support for classes parameterized with types. Such generic classes are particularly useful for the development of collection classes.
Expand Down
2 changes: 1 addition & 1 deletion tutorials/tour/higher-order-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Higher-order Functions
disqus: true

tutorial: scala-tour
num: 18
num: 7
---

Scala allows the definition of higher-order functions. These are functions that _take other functions as parameters_, or whose _result is a function_. Here is a function `apply` which takes another function `f` and a value `v` and applies function `f` to `v`:
Expand Down
2 changes: 1 addition & 1 deletion tutorials/tour/implicit-parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Implicit Parameters
disqus: true

tutorial: scala-tour
num: 10
num: 24
---

A method with _implicit parameters_ can be applied to arguments just like a normal method. In this case the implicit label has no effect. However, if such a method misses arguments for its implicit parameters, such arguments will be automatically provided.
Expand Down
2 changes: 1 addition & 1 deletion tutorials/tour/inner-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Inner Classes
disqus: true

tutorial: scala-tour
num: 11
num: 20
---

In Scala it is possible to let classes have other classes as members. Opposed to Java-like languages where such inner classes are members of the enclosing class, in Scala such inner classes are bound to the outer object. To illustrate the difference, we quickly sketch the implementation of a graph datatype:
Expand Down
2 changes: 1 addition & 1 deletion tutorials/tour/local-type-inference.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Local Type Inference
disqus: true

tutorial: scala-tour
num: 29
num: 27
---
Scala has a built-in type inference mechanism which allows the programmer to omit certain type annotations. It is, for instance, often not necessary in Scala to specify the type of a variable, since the compiler can deduce the type from the initialization expression of the variable. Also return types of methods can often be omitted since they corresponds to the type of the body, which gets inferred by the compiler.

Expand Down
2 changes: 1 addition & 1 deletion tutorials/tour/lower-type-bounds.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Lower Type Bounds
disqus: true

tutorial: scala-tour
num: 26
num: 19
---

While [upper type bounds](upper-type-bounds.html) limit a type to a subtype of another type, *lower type bounds* declare a type to be a supertype of another type. The term `T >: A` expresses that the type parameter `T` or the abstract type `T` refer to a supertype of type `A`.
Expand Down
2 changes: 1 addition & 1 deletion tutorials/tour/mixin-class-composition.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Mixin Class Composition
disqus: true

tutorial: scala-tour
num: 12
num: 5
---

As opposed to languages that only support _single inheritance_, Scala has a more general notion of class reuse. Scala makes it possible to reuse the _new member definitions of a class_ (i.e. the delta in relationship to the superclass) in the definition of a new class. This is expressed as a _mixin-class composition_. Consider the following abstraction for iterators.
Expand Down
2 changes: 1 addition & 1 deletion tutorials/tour/named-parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Named Parameters
disqus: true

tutorial: scala-tour
num: 35
num: 32
---

When calling methods and functions, you can use the name of the variables expliclty in the call, like so:
Expand Down
2 changes: 1 addition & 1 deletion tutorials/tour/nested-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Nested Functions
disqus: true

tutorial: scala-tour
num: 13
num: 8
---

In Scala it is possible to nest function definitions. The following object provides a `filter` function for extracting values from a list of integers that are below a threshold value:
Expand Down
2 changes: 1 addition & 1 deletion tutorials/tour/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Operators
disqus: true

tutorial: scala-tour
num: 17
num: 28
---

Any method which takes a single parameter can be used as an *infix operator* in Scala. Here is the definition of class `MyBool` which defines three methods `and`, `or`, and `negate`.
Expand Down
2 changes: 1 addition & 1 deletion tutorials/tour/pattern-matching.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Pattern Matching
disqus: true

tutorial: scala-tour
num: 20
num: 11
---

Scala has a built-in general pattern matching mechanism. It allows to match on any sort of data with a first-match policy.
Expand Down
2 changes: 1 addition & 1 deletion tutorials/tour/polymorphic-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Polymorphic Methods
disqus: true

tutorial: scala-tour
num: 21
num: 26
---

Methods in Scala can be parameterized with both values and types. Like on the class level, value parameters are enclosed in a pair of parentheses, while type parameters are declared within a pair of brackets.
Expand Down
2 changes: 1 addition & 1 deletion tutorials/tour/regular-expression-patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Regular Expression Patterns
disqus: true

tutorial: scala-tour
num: 22
num: 13
---

## Right-ignoring sequence patterns ##
Expand Down
2 changes: 1 addition & 1 deletion tutorials/tour/sequence-comprehensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Sequence Comprehensions
disqus: true

tutorial: scala-tour
num: 7
num: 15
---

Scala offers a lightweight notation for expressing *sequence comprehensions*. Comprehensions have the form `for (enumerators) yield e`, where `enumerators` refers to a semicolon-separated list of enumerators. An *enumerator* is either a generator which introduces new variables, or it is a filter. A comprehension evaluates the body `e` for each binding generated by the enumerators and returns a sequence of these values.
Expand Down
6 changes: 3 additions & 3 deletions tutorials/tour/tour-of-scala.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Scala is a pure object-oriented language in the sense that [every value is an ob
## Scala is functional ##
Scala is also a functional language in the sense that [every function is a value](unified-types.html). Scala provides a [lightweight syntax](anonymous-function-syntax.html) for defining anonymous functions, it supports [higher-order functions](higher-order-functions.html), it allows functions to be [nested](nested-functions.html), and supports [currying](currying.html). Scala's [case classes](case-classes.html) and its built-in support for [pattern matching](pattern-matching.html) model algebraic types used in many functional programming languages.

Furthermore, Scala's notion of pattern matching naturally extends to the [processing of XML data](xml-processing.html) with the help of [right-ignoring sequence patterns](regular-expression-patterns.html). In this context, [sequence comprehensions](sequence-comprehensions.html) are useful for formulating queries. These features make Scala ideal for developing applications like web services.
Furthermore, Scala's notion of pattern matching naturally extends to the [processing of XML data](xml-processing.html) with the help of [right-ignoring sequence patterns](regular-expression-patterns.html), by way of general extension via [extractor objects](extractor-objects.html). In this context, [sequence comprehensions](sequence-comprehensions.html) are useful for formulating queries. These features make Scala ideal for developing applications like web services.

## Scala is statically typed ##
Scala is equipped with an expressive type system that enforces statically that abstractions are used in a safe and coherent manner. In particular, the type system supports:
Expand All @@ -26,7 +26,7 @@ Scala is equipped with an expressive type system that enforces statically that a
* [inner classes](inner-classes.html) and [abstract types](abstract-types.html) as object members
* [compound types](compound-types.html)
* [explicitly typed self references](explicitly-typed-self-references.html)
* [views](views.html)
* [implicit parameters](implicit-parameters.html) and [views](views.html)
* [polymorphic methods](polymorphic-methods.html)

A [local type inference mechanism](local-type-inference.html) takes care that the user is not required to annotate the program with redundant type information. In combination, these features provide a powerful basis for the safe reuse of programming abstractions and for the type-safe extension of software.
Expand All @@ -40,6 +40,6 @@ In practice, the development of domain-specific applications often requires doma
A joint use of both features facilitates the definition of new statements without extending the syntax and without using macro-like meta-programming facilities.

Scala interoperates with Java and .NET.
Scala is designed to interoperate well with the popular Java 2 Runtime Environment (JRE). In particular, the interaction with the mainstream object-oriented Java programming language is as smooth as possible. Scala has the same compilation model (separate compilation, dynamic class loading) like Java and allows access to thousands of existing high-quality libraries. Support for the .NET Framework (CLR) is also available.
Scala is designed to interoperate well with the popular Java 2 Runtime Environment (JRE). In particular, the interaction with the mainstream object-oriented Java programming language is as smooth as possible. Newer Java features like [annotations](annotations.html) and Java generics have direct analogues in Scala. Those Scala features without Java analogues, such as [default](default-parameter-values.html) and [named parameters](named-parameters.html), compile as close to Java as they can reasonably come. Scala has the same compilation model (separate compilation, dynamic class loading) like Java and allows access to thousands of existing high-quality libraries. Support for the .NET Framework (CLR) is also available.

Please continue to the next page to read more.
2 changes: 1 addition & 1 deletion tutorials/tour/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Traits
disqus: true

tutorial: scala-tour
num: 24
num: 4
---

Similar to interfaces in Java, traits are used to define object types by specifying the signature of the supported methods. Unlike Java, Scala allows traits to be partially implemented; i.e. it is possible to define default implementations for some methods. In contrast to classes, traits may not have constructor parameters.
Expand Down
2 changes: 1 addition & 1 deletion tutorials/tour/unified-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Unified Types
disqus: true

tutorial: scala-tour
num: 30
num: 2
---

In contrast to Java, all values in Scala are objects (including numerical values and functions). Since Scala is class-based, all values are instances of a class. The diagram below illustrates the class hierarchy.
Expand Down
2 changes: 1 addition & 1 deletion tutorials/tour/upper-type-bounds.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Upper Type Bounds
disqus: true

tutorial: scala-tour
num: 25
num: 18
---

In Scala, [type parameters](generic-classes.html) and [abstract types](abstract-types.html) may be constrained by a type bound. Such type bounds limit the concrete values of the type variables and possibly reveal more information about the members of such types. An _upper type bound_ `T <: A` declares that type variable `T` refers to a subtype of type `A`.
Expand Down
Loading