Попытка написать читаемый код на C# на основе сказки о Золотой Рыбке

в 17:38, , рубрики: Песочница, метки: , ,

Всегда поддерживал мнение о т ом, что код должен читаться легко, как стихи.
Эта публикация может быть рассмотрена попытка сохранить плавность чтения при переводе известной сказки с русского на C#.

Goldfish.cs

/// <summary>
/// GoldFish.Net 
/// Classic Fairy Tale translated to C# by RVK
/// </summary>
namespace Pushkin.A.S
{
    struct EarthHouse<People>: Home, Beside<BlueSea> where People : Poor
    {        
        [Old] People OldMan, OldWoman;

        private FishingGear seine;
        volatile Catch fish;
        [Broken] WashTub washTub;

        [Duration( 30 + 3 )] void OnceUpon( DateTime time )
        {
            OldMan.Fishing( seine, out fish );
            OldWoman.Spinning( new Thread( () => { } ) );
        }

        public static event Story GoldFishFairyTale = async (That, OldMan, GoldFish, OldWoman ) => {
            try {
                using( var sein = That.seine ) {
                    var firstResult = await OldMan.Throw( sein ); Assert.That( firstResult is Sea.Slime );
                    var secndResult = await OldMan.Throw( sein ); Assert.That( secndResult is Sea.Weed );
                    var thirdResult = await OldMan.Throw( sein ); Assert.That( thirdResult is Fish );
                    
                    Assert.That( thirdResult is GoldFish );

                    thirdResult.SayTo( OldMan, ( me, you ) => {
                        if( you.Release( me ) ) {
                            me.Give( you, any : ( thing ) =>
                                from what in you.WishList where you.WishList.Count == 3 select what );
                        }
                    } );
                }
            }
            catch( FishSpeaksRussianException strange ) {
                OldMan.ReplyTo( strange.Catch as GoldFish, ( I, your ) => { 
                    Assert.That( I.DontWant( your.Repayment ) ); 
                } ); 
                OldMan.Release( strange.Catch as GoldFish ); 
            }
            finally {
                OldMan.Come<Home>(); 
                OldMan.Tell( OldWoman, GoldFishFairyTale );
                That.OldWoman.CastTo< IDiot >( OldMan );
                That.OldWoman.CastTo< IMbecile >( OldMan );
                That.OldWoman.Say( ( you, OldMan ) => {
                    you.Should( () => { 
BlueSea:
                        goto BlueSea; // Caution: Infinite loop
                        you.Ask( ( ( Fucking ) That.fish ), () => ( Fucking ) new WashTub() );
                    } );
                });
            }
        };
    }
}

Поделиться новостью

* - обязательные к заполнению поля