# vuepress-plugin-code-switcher
This is the demo page for the vuepress-plugin-code-switcher
. Please refer to
the Readme on its GitHub
Repository (opens new window) for more
information regarding how to use it.
All examples shown are taken from Rosetta Code (opens new window).
# Synchronised switchers
These two switchers are synchronized by default, no additional settings needed to be made.
# Longest Common Substring
- Julia
- Kotlin
- Perl
function lcs(s1::AbstractString, s2::AbstractString)
l, r = 1, 0
sub_len = 0
for i in 1:length(s1)
for j in i:length(s1)
if !contains(s2, SubString(s1, i, j)) break
elseif sub_len < j - i
l, r = i, j
sub_len = j - i
end
end
end
s1[l:r]
end
@show lcs("thisisatest", "testing123testing")
# Palindrome Detection
- Julia
- Kotlin
- Perl
function palindrome(s)
len = length(s)
if(len==0 || len==1)
return true
end
if(s[1] == s[len])
return palindrome(SubString(s,2,len-1))
end
return false
end
# Isolated Switchers
The following two switchers are isolated. They do not listen to any of the other switchers being changed.
# Letter Frequency
- Kotlin
- Ruby
import java.io.File
fun main(args: Array<String>) {
val text = File("input.txt").readText().toLowerCase()
val letterMap = text.filter { it in 'a'..'z' }.groupBy { it }.toSortedMap()
for (letter in letterMap) println("${letter.key} = ${letter.value.size}")
val sum = letterMap.values.sumBy { it.size }
println("\nTotal letters = $sum")
}
# Rot-13
- Kotlin
- Ruby
import java.io.*
fun String.rot13() = map {
when {
it.isUpperCase() -> { val x = it + 13; if (x > 'Z') x - 26 else x }
it.isLowerCase() -> { val x = it + 13; if (x > 'z') x - 26 else x }
else -> it
} }.toCharArray()
fun InputStreamReader.println() =
try { BufferedReader(this).forEachLine { println(it.rot13()) } }
catch (e: IOException) { e.printStackTrace() }
fun main(args: Array<String>) {
if (args.any())
args.forEach { FileReader(it).println() }
else
InputStreamReader(System.`in`).println()
}
# Multiple Switcher Groups
The following switchers are in different groups. Even though they show the same languages, they might not be thematically related, so only some code blocks switch, while the others stay the same.
# Group 1: FizzBuzz
- Nim
- OCaml
for i in 1..100:
if i mod 15 == 0:
echo("FizzBuzz")
elif i mod 3 == 0:
echo("Fizz")
elif i mod 5 == 0:
echo("Buzz")
else:
echo(i)
# Group 1: Hello world
- Nim
- OCaml
echo("Hello world!")
# Group 2: String case
- Nim
- OCaml
import strutils
var s: string = "alphaBETA_123"
echo s," as upper case: ", toUpper(s)
echo s," as lower case: ", toLower(s)
echo s," as Capitalized: ", capitalize(s)
echo s," as normal case: ", normalize(s) # remove underscores, toLower
# Group 2: Strip whitespace
- Nim
- OCaml
import strutils
let s = " \t \n String with spaces \t \n "
echo "'", s, "'"
echo "'", s.strip(trailing = false), "'"
echo "'", s.strip(leading = false), "'"
echo "'", s.strip(), "'"