en no

y-combinator-izing curried angular router

12/5/2020 ca20d748f80df691ee3a65f51d85e144

i've recently gone a bit mad with wanting to turn everything i touch into functional code & this being my latest foray, a thing that creates a flat routing structure from nested functions and stuff:

from this code

const APP_ROUTES:Routes = new AppRouter()
  .register(``,                                         FeedComponent,             LoggedInGuard)
  .register(`watch`,                                    PerformanceWatchComponent, LoggedInGuard)
  .register(`search`,                                   CatalogComponent,          LoggedInGuard)
  .register(`verified`,                                 VerifiedComponent)
  .register(`user/@:${RP.UserId}`,                      ProfileComponent,          LoggedInGuard)
  .register(`performance/:${RP.PerformanceId}`,         PerformanceComponent,      LoggedInGuard)
  .pushRouter(r => r.register(`host/@:${RP.HostId}`,    HostComponent, LoggedInGuard))
    .register(`performances`,                           HostPerformancesComponent, LoggedInGuard)
    .pushRouter(r => r.register(`:${RP.PerformanceId}`, HostPerformancesComponent, LoggedInGuard))
      .register(`watch`,                                HostPerformancesComponent, LoggedInGuard)
  .register(`ui`,                                       TestbedComponent)

all the fp goodness man could ever wish for, i mean look at that supple tree...and the y-combinator itself you may ask?

export default class AppRouter {
  // ...

  apply():Routes {
    return Y((r:(x:AppRouter) => Routes) => n => {
      return [ => ({, path: n.root + rs.path })),
        ...(n.routers.reduce((acc, curr) => {
          // omgggg
          return [...acc, ...r(curr)];
        }, [])),

gone are the days of stupid objects and arrays