diff --git a/naive.go b/naive.go deleted file mode 100644 index cde236c..0000000 --- a/naive.go +++ /dev/null @@ -1,88 +0,0 @@ -// SPDX-License-Identifier: Unlicense - -package gigaparsec - -func Bind2Naïve[In, Out, T, T2 any]( - p Parser[In, T], - f func(T) Parser[In, T2], - f2 func(T2) Parser[In, Out], -) Parser[In, Out] { - return Bind(Bind(p, f), f2) -} -func Bind3Naïve[In, Out, T, T2, T3, T4, T5, T6, T7, T8, T9 any]( - p Parser[In, T], - f func(T) Parser[In, T2], - f2 func(T2) Parser[In, T3], - f3 func(T3) Parser[In, Out], -) Parser[In, Out] { - return Bind(Bind(Bind(p, f), f2), f3) -} -func Bind4Naïve[In, Out, T, T2, T3, T4 any]( - p Parser[In, T], - f func(T) Parser[In, T2], - f2 func(T2) Parser[In, T3], - f3 func(T3) Parser[In, T4], - f4 func(T4) Parser[In, Out], -) Parser[In, Out] { - return Bind(Bind(Bind(Bind(p, f), f2), f3), f4) -} -func Bind5Naïve[In, Out, T, T2, T3, T4, T5 any]( - p Parser[In, T], - f func(T) Parser[In, T2], - f2 func(T2) Parser[In, T3], - f3 func(T3) Parser[In, T4], - f4 func(T4) Parser[In, T5], - f5 func(T5) Parser[In, Out], -) Parser[In, Out] { - return Bind(Bind(Bind(Bind(Bind(p, f), f2), f3), f4), f5) -} -func Bind6Naïve[In, Out, T, T2, T3, T4, T5, T6 any]( - p Parser[In, T], - f func(T) Parser[In, T2], - f2 func(T2) Parser[In, T3], - f3 func(T3) Parser[In, T4], - f4 func(T4) Parser[In, T5], - f5 func(T5) Parser[In, T6], - f6 func(T6) Parser[In, Out], -) Parser[In, Out] { - return Bind(Bind(Bind(Bind(Bind(Bind(p, f), f2), f3), f4), f5), f6) -} -func Bind7Naïve[In, Out, T, T2, T3, T4, T5, T6, T7 any]( - p Parser[In, T], - f func(T) Parser[In, T2], - f2 func(T2) Parser[In, T3], - f3 func(T3) Parser[In, T4], - f4 func(T4) Parser[In, T5], - f5 func(T5) Parser[In, T6], - f6 func(T6) Parser[In, T7], - f7 func(T7) Parser[In, Out], -) Parser[In, Out] { - return Bind(Bind(Bind(Bind(Bind(Bind(Bind(p, f), f2), f3), f4), f5), f6), f7) -} -func Bind8Naïve[In, Out, T, T2, T3, T4, T5, T6, T7, T8 any]( - p Parser[In, T], - f func(T) Parser[In, T2], - f2 func(T2) Parser[In, T3], - f3 func(T3) Parser[In, T4], - f4 func(T4) Parser[In, T5], - f5 func(T5) Parser[In, T6], - f6 func(T6) Parser[In, T7], - f7 func(T7) Parser[In, T8], - f8 func(T8) Parser[In, Out], -) Parser[In, Out] { - return Bind(Bind(Bind(Bind(Bind(Bind(Bind(Bind(p, f), f2), f3), f4), f5), f6), f7), f8) -} -func Bind9Naïve[In, Out, T, T2, T3, T4, T5, T6, T7, T8, T9 any]( - p Parser[In, T], - f func(T) Parser[In, T2], - f2 func(T2) Parser[In, T3], - f3 func(T3) Parser[In, T4], - f4 func(T4) Parser[In, T5], - f5 func(T5) Parser[In, T6], - f6 func(T6) Parser[In, T7], - f7 func(T7) Parser[In, T8], - f8 func(T8) Parser[In, T9], - f9 func(T9) Parser[In, Out], -) Parser[In, Out] { - return Bind(Bind(Bind(Bind(Bind(Bind(Bind(Bind(Bind(p, f), f2), f3), f4), f5), f6), f7), f8), f9) -} diff --git a/naive/naive.go b/naive/naive.go new file mode 100644 index 0000000..82466bb --- /dev/null +++ b/naive/naive.go @@ -0,0 +1,92 @@ +// SPDX-License-Identifier: Unlicense + +// Package naive contains naïve implementations of the Bind and Seq combinators. +// The accompanying tests include simple benchmarks comparing their performance. +package naive + +import gp "git.codemonkeysoftware.net/b/gigaparsec" + +func Bind2[In, Out, T, T2 any]( + p gp.Parser[In, T], + f func(T) gp.Parser[In, T2], + f2 func(T2) gp.Parser[In, Out], +) gp.Parser[In, Out] { + return gp.Bind(gp.Bind(p, f), f2) +} +func Bind3[In, Out, T, T2, T3, T4, T5, T6, T7, T8, T9 any]( + p gp.Parser[In, T], + f func(T) gp.Parser[In, T2], + f2 func(T2) gp.Parser[In, T3], + f3 func(T3) gp.Parser[In, Out], +) gp.Parser[In, Out] { + return gp.Bind(gp.Bind(gp.Bind(p, f), f2), f3) +} +func Bind4[In, Out, T, T2, T3, T4 any]( + p gp.Parser[In, T], + f func(T) gp.Parser[In, T2], + f2 func(T2) gp.Parser[In, T3], + f3 func(T3) gp.Parser[In, T4], + f4 func(T4) gp.Parser[In, Out], +) gp.Parser[In, Out] { + return gp.Bind(gp.Bind(gp.Bind(gp.Bind(p, f), f2), f3), f4) +} +func Bind5[In, Out, T, T2, T3, T4, T5 any]( + p gp.Parser[In, T], + f func(T) gp.Parser[In, T2], + f2 func(T2) gp.Parser[In, T3], + f3 func(T3) gp.Parser[In, T4], + f4 func(T4) gp.Parser[In, T5], + f5 func(T5) gp.Parser[In, Out], +) gp.Parser[In, Out] { + return gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(p, f), f2), f3), f4), f5) +} +func Bind6[In, Out, T, T2, T3, T4, T5, T6 any]( + p gp.Parser[In, T], + f func(T) gp.Parser[In, T2], + f2 func(T2) gp.Parser[In, T3], + f3 func(T3) gp.Parser[In, T4], + f4 func(T4) gp.Parser[In, T5], + f5 func(T5) gp.Parser[In, T6], + f6 func(T6) gp.Parser[In, Out], +) gp.Parser[In, Out] { + return gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(p, f), f2), f3), f4), f5), f6) +} +func Bind7[In, Out, T, T2, T3, T4, T5, T6, T7 any]( + p gp.Parser[In, T], + f func(T) gp.Parser[In, T2], + f2 func(T2) gp.Parser[In, T3], + f3 func(T3) gp.Parser[In, T4], + f4 func(T4) gp.Parser[In, T5], + f5 func(T5) gp.Parser[In, T6], + f6 func(T6) gp.Parser[In, T7], + f7 func(T7) gp.Parser[In, Out], +) gp.Parser[In, Out] { + return gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(p, f), f2), f3), f4), f5), f6), f7) +} +func Bind8[In, Out, T, T2, T3, T4, T5, T6, T7, T8 any]( + p gp.Parser[In, T], + f func(T) gp.Parser[In, T2], + f2 func(T2) gp.Parser[In, T3], + f3 func(T3) gp.Parser[In, T4], + f4 func(T4) gp.Parser[In, T5], + f5 func(T5) gp.Parser[In, T6], + f6 func(T6) gp.Parser[In, T7], + f7 func(T7) gp.Parser[In, T8], + f8 func(T8) gp.Parser[In, Out], +) gp.Parser[In, Out] { + return gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(p, f), f2), f3), f4), f5), f6), f7), f8) +} +func Bind9[In, Out, T, T2, T3, T4, T5, T6, T7, T8, T9 any]( + p gp.Parser[In, T], + f func(T) gp.Parser[In, T2], + f2 func(T2) gp.Parser[In, T3], + f3 func(T3) gp.Parser[In, T4], + f4 func(T4) gp.Parser[In, T5], + f5 func(T5) gp.Parser[In, T6], + f6 func(T6) gp.Parser[In, T7], + f7 func(T7) gp.Parser[In, T8], + f8 func(T8) gp.Parser[In, T9], + f9 func(T9) gp.Parser[In, Out], +) gp.Parser[In, Out] { + return gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(p, f), f2), f3), f4), f5), f6), f7), f8), f9) +} diff --git a/naïve_test.go b/naive/naive_test.go similarity index 83% rename from naïve_test.go rename to naive/naive_test.go index 9bb3f0a..1b35ef9 100644 --- a/naïve_test.go +++ b/naive/naive_test.go @@ -1,11 +1,12 @@ // SPDX-License-Identifier: Unlicense -package gigaparsec_test +package naive_test import ( "testing" "git.codemonkeysoftware.net/b/gigaparsec" + "git.codemonkeysoftware.net/b/gigaparsec/naive" ) func BenchmarkBind5(b *testing.B) { @@ -19,15 +20,15 @@ func BenchmarkBind5(b *testing.B) { return bind5(gigaparsec.Match(byte(0)), f, f, f, f, f) } input := gigaparsec.SliceReaderAt[byte]{0} - b.Run("Bind5", func(b *testing.B) { + b.Run("gigaparsec.Bind5", func(b *testing.B) { for range b.N { gigaparsec.Run(p(gigaparsec.Bind5), input) } }) - b.Run("Bind5Naïve", func(b *testing.B) { + b.Run("naïve.Bind5", func(b *testing.B) { for range b.N { - gigaparsec.Run(p(gigaparsec.Bind5Naïve), input) + gigaparsec.Run(p(naive.Bind5), input) } }) } @@ -43,15 +44,15 @@ func BenchmarkBind9(b *testing.B) { return bind5(gigaparsec.Match(byte(0)), f, f, f, f, f, f, f, f, f) } input := gigaparsec.SliceReaderAt[byte]{0} - b.Run("Bind9", func(b *testing.B) { + b.Run("gigaparsec.Bind9", func(b *testing.B) { for range b.N { gigaparsec.Run(p(gigaparsec.Bind9), input) } }) - b.Run("Bind9Naïve", func(b *testing.B) { + b.Run("naive.Bind9", func(b *testing.B) { for range b.N { - gigaparsec.Run(p(gigaparsec.Bind9Naïve), input) + gigaparsec.Run(p(naive.Bind9), input) } }) }