Desi Cochrane преди 5 години
ревизия
50cb7d2c48
променени са 5 файла, в които са добавени 278 реда и са изтрити 0 реда
  1. +11
    -0
      go.mod
  2. +6
    -0
      go.sum
  3. +100
    -0
      main.go
  4. +71
    -0
      sample.md
  5. +90
    -0
      test.html

+ 11
- 0
go.mod Целия файл

@@ -0,0 +1,11 @@
module github.com/desicochrane/publish

go 1.12

require (
github.com/Ambrevar/blackfriday-latex v0.0.0-20171128113613-dc387d576233
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
gopkg.in/russross/blackfriday.v2 v2.0.1
)

replace gopkg.in/russross/blackfriday.v2 => github.com/russross/blackfriday/v2 v2.0.1

+ 6
- 0
go.sum Целия файл

@@ -0,0 +1,6 @@
github.com/Ambrevar/blackfriday-latex v0.0.0-20171128113613-dc387d576233 h1:N74lSeC1l+zNx13jAAh1uSk3RUpRpvrPWvq5cDyUM6M=
github.com/Ambrevar/blackfriday-latex v0.0.0-20171128113613-dc387d576233/go.mod h1:Gq7GVkiUFiOVot5Ycu4gCUkqGByvd0DlkWLRg+ENjwQ=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=

+ 100
- 0
main.go Целия файл

@@ -0,0 +1,100 @@
package main

import (
"bytes"
"fmt"
"gopkg.in/russross/blackfriday.v2"
"html/template"
"io"
"io/ioutil"
"net/http"
"os"
)

func main() {
if len(os.Args) != 2 {
fmt.Printf("usage: %s <document>", os.Args[0])
os.Exit(1)
}

tpl, err := template.New("dummy").Parse(`
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.11.0/dist/katex.min.css" integrity="sha384-BdGj8xC2eZkQaxoQ8nSLefg4AV4/AwB3Fj+8SUSo7pnKP6Eoy18liIKTPn9oBYNG" crossorigin="anonymous">

<script defer src="https://cdn.jsdelivr.net/npm/katex@0.11.0/dist/katex.min.js" integrity="sha384-JiKN5O8x9Hhs/UE5cT5AAJqieYlOZbGT3CHws/y97o3ty4R7/O5poG9F3JoiOYw1" crossorigin="anonymous"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.11.0/dist/contrib/auto-render.min.js" integrity="sha384-kWPLUVMOks5AQFrykwIup5lo0m3iMkkHrD0uJ4H5cjeGihAutqP0yW0J6dpFiVkI" crossorigin="anonymous"
onload="renderMathInElement(document.body);"></script>

<style>
.katex { font-size: 1em; }
</style>
</head>
<body>
{{.}}
</body>
</html>
`)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
f, err := os.Open(os.Args[1])
if err != nil {
panic(err)
}

b, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}

unsafe := blackfriday.Run(b, blackfriday.WithRenderer(NewMyRenderer()))
if err := tpl.Execute(w, template.HTML(unsafe)); err != nil {
panic(err)
}
}))
fmt.Println("listening on :19000")
http.ListenAndServe(":19000", nil)
}

type MyRenderer struct {
html *blackfriday.HTMLRenderer
}

func NewMyRenderer() *MyRenderer {
return &MyRenderer{
html: blackfriday.NewHTMLRenderer(blackfriday.HTMLRendererParameters{
Flags: blackfriday.CommonHTMLFlags,
}),
}
}

func (r *MyRenderer) RenderNode(w io.Writer, node *blackfriday.Node, entering bool) blackfriday.WalkStatus {
if node.Type == blackfriday.Code {
if bytes.HasPrefix(node.Literal, []byte("\\(")) && bytes.HasSuffix(node.Literal, []byte("\\)")) {
w.Write(node.Literal)
return blackfriday.GoToNext
}
} else if node.Type == blackfriday.CodeBlock && string(node.CodeBlockData.Info) == "katex" {
w.Write([]byte("\n<p>"))
w.Write(node.Literal)
w.Write([]byte("</p>"))
return blackfriday.GoToNext
}
return r.html.RenderNode(w, node, entering)
}

func (r *MyRenderer) RenderHeader(w io.Writer, ast *blackfriday.Node) {
r.html.RenderHeader(w, ast)
}
func (r *MyRenderer) RenderFooter(w io.Writer, ast *blackfriday.Node) {
r.html.RenderHeader(w, ast)
}

+ 71
- 0
sample.md Целия файл

@@ -0,0 +1,71 @@
# The Fibonacci Problem

### Introduction
In this article we will investigate the famous Fibonacci sequence from the perspective of tail-recursion. We will see that the Fibonacci sequence requires more advanced reasoning than the problem we explored in a [previous article](2018-02-22-gopher-forth-and-multiply.html) and we will practice new tactics in our pursuit of refactoring to a strict tail-recursive solution.

We will use Golang for its built-in benchmarking, but it should be easy to follow even if you've never seen Golang before.

### The Problem
Over 800 years ago Leonardo of Pisa posed a question that went something like this:

> Suppose we have a newly-born fluffy. Fluffies are magical creatures - they never die and when they are one month old they become adult fluffies. One month after a fluffy has become an adult it will produce another newly-born fluffy - and then again every month after. Given this process repeats for every newly-born fluffy, how many fluffies will there be after one year has passed?

If we then denote the number of fluffies in a given month `\(n\)` as `\(F_n\)` then our task is to find `\( F_{12} \)`.

As a start, we make the observation that the number of fluffies in any given month is the sum of the adult fluffies and baby fluffies in that month. If we denote the number of adult fluffies and baby fluffies in month `\(n\)` as `\(A_n\)` and `\(B_n\)` respectively, we have that:

```katex
\[ F_n = A_n + B_n \]
```
Let's first reason about the number of **adult** fluffies in a given month `\(n\)`. Since fluffies never die, all the adult fluffies from the previous month `\( (n-1) \)` will still exist in the current month `\(n\)`. Also, fluffies become adults after only one month, so all the baby fluffies from the previous month have now also become adult fluffies in the current month.

Thus we have that the number of adult fluffies in a given month `\(n\)` will be the sum of the adult fluffies and baby fluffies from the previous month `\((n-1)\)`:

```katex
\[ \begin{aligned}
A_n &= A_{n-1} + B_{n-1} & \\
&= F_{n-1} & (\text{above definition of } F_n)
\end{aligned} \]
```

Next, let's examine the **baby** fluffies in a given month. Because each adult fluffy from the previous month will produce a single baby fluffy the next month we have that the number of baby fluffies in a any given month must equal to the number of adult fluffies in the previous month:
```katex
\[ \begin{aligned}
B_{n} &= A_{n-1} \\
&= F_{n-2} & (\text{above definition of } A_n)
\end{aligned} \]
```

With this improved understanding of `\(A_n\)` and `\(B_n\)` we can re-write our definition of `\(F_n\)`:

```katex
\[ \begin{aligned}
F_n &= A_{n} + B_{n} \\
&= F_{n-1} + F_{n-2}
\end{aligned} \]
```

This means that the number of fluffies in a given month is determined by the number of fluffies in the previous two months. Thus if we know the number of fluffies in the first two months we can determine the number of fluffies in any subsequent month. We know in the first month we start with a single fluffy, that is `\(F_1 = 1\)`, and we can say that before the first month we had no fluffies at all so we know that `\(F_0 = 0\)`.

This gives us all the pieces we need to define `\(F_n\)` with a recursive relation:

```katex
\[ \begin{aligned}
F_0 &= 0 \\
F_1 &= 1 \\
F_n &= F_{n-1} + F_{n-2} \text{ where } n > 1
\end{aligned} \]
```

To solve Leonardo of Pisa's problem then we can use simply use the definition to compute from `\(F_0\)` to `\(F_{12}\)`:

```katex
\[ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 \]
```

```katex
\[ \therefore F_{12} = 144 \]
```

This is known as the **Fibonacci sequence** and the `\(n^{th}\)` number in the sequence is known as the `\(n^{th}\)` **Fibonacci number**.

+ 90
- 0
test.html Целия файл

@@ -0,0 +1,90 @@

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.11.0/dist/katex.min.css" integrity="sha384-BdGj8xC2eZkQaxoQ8nSLefg4AV4/AwB3Fj+8SUSo7pnKP6Eoy18liIKTPn9oBYNG" crossorigin="anonymous">

<script defer src="https://cdn.jsdelivr.net/npm/katex@0.11.0/dist/katex.min.js" integrity="sha384-JiKN5O8x9Hhs/UE5cT5AAJqieYlOZbGT3CHws/y97o3ty4R7/O5poG9F3JoiOYw1" crossorigin="anonymous"></script>

<script defer src="https://cdn.jsdelivr.net/npm/katex@0.11.0/dist/contrib/auto-render.min.js" integrity="sha384-kWPLUVMOks5AQFrykwIup5lo0m3iMkkHrD0uJ4H5cjeGihAutqP0yW0J6dpFiVkI" crossorigin="anonymous"
onload="renderMathInElement(document.body);"></script>
<style>.katex { font-size: 1em; }
body {
font-family: KaTeX_Main,"Times New Roman",serif;
line-height: 1.2;
text-indent: 0;
text-rendering: auto;
}</style>
</head>
<body>
<h1>The Fibonacci Problem</h1>

<h3>Introduction</h3>

<p>In this article we will investigate the famous Fibonacci sequence from the perspective of tail-recursion. We will see that the Fibonacci sequence requires more advanced reasoning than the problem we explored in a <a href="2018-02-22-gopher-forth-and-multiply.html">previous article</a> and we will practice new tactics in our pursuit of refactoring to a strict tail-recursive solution.</p>

<p>We will use Golang for its built-in benchmarking, but it should be easy to follow even if you&rsquo;ve never seen Golang before.</p>

<h3>The Problem</h3>

<p>Over 800 years ago Leonardo of Pisa posed a question that went something like this:</p>

<blockquote>
<p>Suppose we have a newly-born fluffy. Fluffies are magical creatures - they never die and when they are one month old they become adult fluffies. One month after a fluffy has become an adult it will produce another newly-born fluffy - and then again every month after. Given this process repeats for every newly-born fluffy, how many fluffies will there be after one year has passed?</p>
</blockquote>

<p>If we then denote the number of fluffies in a given month \(n\) as \(F_n\) then our task is to find \( F_{12} \).</p>

<p>As a start, we make the observation that the number of fluffies in any given month is the sum of the adult fluffies and baby fluffies in that month. If we denote the number of adult fluffies and baby fluffies in month \(n\) as \(A_n\) and \(B_n\) respectively, we have that:</p>

<p>\[ F_n = A_n + B_n \]
</p>
<p>Let&rsquo;s first reason about the number of <strong>adult</strong> fluffies in a given month \(n\). Since fluffies never die, all the adult fluffies from the previous month \( (n-1) \) will still exist in the current month \(n\). Also, fluffies become adults after only one month, so all the baby fluffies from the previous month have now also become adult fluffies in the current month.</p>

<p>Thus we have that the number of adult fluffies in a given month \(n\) will be the sum of the adult fluffies and baby fluffies from the previous month \((n-1)\):</p>

<p>\[ \begin{aligned}
A_n &= A_{n-1} + B_{n-1} & \\
&= F_{n-1} & (\text{above definition of } F_n)
\end{aligned} \]
</p>
<p>Next, let&rsquo;s examine the <strong>baby</strong> fluffies in a given month. Because each adult fluffy from the previous month will produce a single baby fluffy the next month we have that the number of baby fluffies in a any given month must equal to the number of adult fluffies in the previous month:</p>

<p>\[ \begin{aligned}
B_{n} &= A_{n-1} \\
&= F_{n-2} & (\text{above definition of } A_n)
\end{aligned} \]
</p>
<p>With this improved understanding of \(A_n\) and \(B_n\) we can re-write our definition of \(F_n\):</p>

<p>\[ \begin{aligned}
F_n &= A_{n} + B_{n} \\
&= F_{n-1} + F_{n-2}
\end{aligned} \]
</p>
<p>This means that the number of fluffies in a given month is determined by the number of fluffies in the previous two months. Thus if we know the number of fluffies in the first two months we can determine the number of fluffies in any subsequent month. We know in the first month we start with a single fluffy, that is \(F_1 = 1\), and we can say that before the first month we had no fluffies at all so we know that \(F_0 = 0\).</p>

<p>This gives us all the pieces we need to define \(F_n\) with a recursive relation:</p>

<p>\[ \begin{aligned}
F_0 &= 0 \\
F_1 &= 1 \\
F_n &= F_{n-1} + F_{n-2} \text{ where } n > 1
\end{aligned} \]
</p>
<p>To solve Leonardo of Pisa&rsquo;s problem then we can use simply use the definition to compute from \(F_0\) to \(F_{12}\):</p>

<p>\[ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 \]
</p>
<p>\[ \therefore F_{12} = 144 \]
</p>
<p>This is known as the <strong>Fibonacci sequence</strong> and the \(n^{th}\) number in the sequence is known as the \(n^{th}\) <strong>Fibonacci number</strong>.</p>

</body>
</html>

Loading…
Отказ
Запис