Move naïve Bind and Seq into a separate package

This commit is contained in:
Brandon Dyck 2024-11-29 00:53:11 -07:00
parent fa6c15566d
commit c85d0d280e
3 changed files with 100 additions and 95 deletions

View File

@ -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)
}

92
naive/naive.go Normal file
View File

@ -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)
}

View File

@ -1,11 +1,12 @@
// SPDX-License-Identifier: Unlicense // SPDX-License-Identifier: Unlicense
package gigaparsec_test package naive_test
import ( import (
"testing" "testing"
"git.codemonkeysoftware.net/b/gigaparsec" "git.codemonkeysoftware.net/b/gigaparsec"
"git.codemonkeysoftware.net/b/gigaparsec/naive"
) )
func BenchmarkBind5(b *testing.B) { 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) return bind5(gigaparsec.Match(byte(0)), f, f, f, f, f)
} }
input := gigaparsec.SliceReaderAt[byte]{0} input := gigaparsec.SliceReaderAt[byte]{0}
b.Run("Bind5", func(b *testing.B) { b.Run("gigaparsec.Bind5", func(b *testing.B) {
for range b.N { for range b.N {
gigaparsec.Run(p(gigaparsec.Bind5), input) 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 { 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) return bind5(gigaparsec.Match(byte(0)), f, f, f, f, f, f, f, f, f)
} }
input := gigaparsec.SliceReaderAt[byte]{0} input := gigaparsec.SliceReaderAt[byte]{0}
b.Run("Bind9", func(b *testing.B) { b.Run("gigaparsec.Bind9", func(b *testing.B) {
for range b.N { for range b.N {
gigaparsec.Run(p(gigaparsec.Bind9), input) 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 { for range b.N {
gigaparsec.Run(p(gigaparsec.Bind9Naïve), input) gigaparsec.Run(p(naive.Bind9), input)
} }
}) })
} }